PageRenderTime 51ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/library/bluetooth/e_bluetooth.c

https://bitbucket.org/andyguest/chatter
C | 721 lines | 546 code | 73 blank | 102 comment | 66 complexity | 821c5422f7e669d7c2941a3047c2d181 MD5 | raw file
  1. /********************************************************************************
  2. Bluetooth for e-puck
  3. Version 1.0 July 2006 Michael Bonani
  4. This file is part of the e-puck library license.
  5. See http://www.e-puck.org/index.php?option=com_content&task=view&id=18&Itemid=45
  6. (c) 2006-2007 Michael Bonani
  7. Robotics system laboratory http://lsro.epfl.ch
  8. Laboratory of intelligent systems http://lis.epfl.ch
  9. Swarm intelligent systems group http://swis.epfl.ch
  10. EPFL Ecole polytechnique federale de Lausanne http://www.epfl.ch
  11. **********************************************************************************/
  12. /*! \file
  13. * \ingroup bluetooth
  14. * \brief Manage Bluetooth.
  15. *
  16. * This module manage the Bluetooth device.
  17. * \author Code: Michael Bonani \n Doc: Jonathan Besuchet
  18. */
  19. /* Bluetooth.c */
  20. #include "./../uart/e_uart_char.h"
  21. #include "./../motor_led/e_epuck_ports.h"
  22. #include "e_bluetooth.h"
  23. #include "stdio.h"
  24. #include "string.h"
  25. #include "stdlib.h"
  26. unsigned char e_bt_local_paired_device[6*8];
  27. struct BtDevice e_bt_present_device[10];
  28. struct BtEPuck e_bt_present_epuck[10];
  29. char local_bt_PIN[4];
  30. /*------ special e-puck fuction ------*/
  31. /*! \brief Try to find other e-puck
  32. *
  33. * This function make global inguiry and check which device are e-puck,
  34. * and list them in globales tables.
  35. * \return number of e-puck found
  36. * \sa e_bt_present_device, e_bt_present_epuck
  37. */
  38. int e_bt_find_epuck(void)
  39. {
  40. int device_find, e_puck_find;
  41. int i,j;
  42. e_puck_find=0;
  43. device_find=e_bt_inquiry(e_bt_present_device);
  44. for (i=0;i<device_find;i++)
  45. {
  46. if((e_bt_present_device[i].class[0]==0)&(e_bt_present_device[i].class[1]==0)&(e_bt_present_device[i].class[2]==0)&(e_bt_present_device[i].address[5]==0x08))//marque of e-puck
  47. {
  48. e_bt_get_friendly_name(&e_bt_present_device[i]);
  49. if((e_bt_present_device[i].friendly_name[0]=='e')&(e_bt_present_device[i].friendly_name[1]=='-')&(e_bt_present_device[i].friendly_name[2]=='p'))
  50. {
  51. for(j=0;j<4;j++)
  52. {
  53. e_bt_present_epuck[e_puck_find].address[j]=e_bt_present_device[i].address[j];//copy address
  54. e_bt_present_epuck[e_puck_find].number[j]=e_bt_present_device[i].friendly_name[j+7];//extract number(=pin)
  55. }
  56. e_bt_present_epuck[e_puck_find].number[4]='\0';//end with null
  57. for(j=4;j<6;j++)
  58. e_bt_present_epuck[e_puck_find].address[j]=e_bt_present_device[i].address[j];
  59. e_puck_find++;
  60. }
  61. }
  62. }
  63. return e_puck_find;
  64. }
  65. /* \brief This function will connect to first e-puck found.
  66. * \return 0 if connect otherwise return error code
  67. */
  68. char e_bt_connect_epuck(void)
  69. {
  70. int e_puck_find;
  71. char error;
  72. e_bt_read_local_pin_number(local_bt_PIN);
  73. e_puck_find=e_bt_find_epuck();
  74. if(e_puck_find)
  75. {
  76. e_bt_write_local_pin_number(&e_bt_present_epuck[0].number[0]);
  77. error=e_bt_etablish_SPP_link(&e_bt_present_epuck[0].address[0]);
  78. e_bt_write_local_pin_number(local_bt_PIN);
  79. return error;
  80. }
  81. else
  82. return 99;
  83. }
  84. /*------ low level bluettoth fuction ------*/
  85. /*! \brief Reset the bluetooth module
  86. * \return The version number
  87. */
  88. int e_bt_reset(void)
  89. {
  90. char send[7];
  91. char read[30];
  92. int i;
  93. char c;
  94. char version[5];
  95. send[0]=0x02; //send reset request
  96. send[1]=0x52;
  97. send[2]=0x26;
  98. send[3]=0x00;
  99. send[4]=0x00;
  100. send[5]=0x78;
  101. send[6]=0x03;
  102. e_send_uart1_char(send,7);
  103. i=0;
  104. c=0;
  105. do
  106. {
  107. if (e_getchar_uart1(&read[i])) //read response
  108. {
  109. c=read[i];
  110. i++;
  111. }
  112. }
  113. while (((char)c != 0x03)||(i<(read[3]+6)));
  114. read[i]='\0';
  115. for(i=0;i<read[6];i++) //extract version
  116. version[i]=read[i+7];
  117. version[i]= '\0';
  118. return atoi(version);
  119. }
  120. /*! \brief Factory reset of the bluetooth module
  121. * \warning use this function only if you are sure, your e-puck must
  122. * be restarted, and renamed!!!
  123. * \return bluetooth error if one occur, 0 otherwise
  124. */
  125. char e_bt_factory_reset(void)
  126. {
  127. char send[8];
  128. char read[10];
  129. int i;
  130. char c;
  131. send[0]=0x02;
  132. send[1]=0x52;
  133. send[2]=0x1A;
  134. send[3]=0x00;
  135. send[4]=0x00;
  136. send[5]=0x6c;
  137. send[6]=0x03;//link number 1-30
  138. e_send_uart1_char(send,7);
  139. i=0;
  140. c=0;
  141. do
  142. {
  143. if (e_getchar_uart1(&read[i])) //read response
  144. {
  145. c=read[i];
  146. i++;
  147. }
  148. }
  149. while (((char)c != 0x03)||(i<(read[3]+6)));
  150. read[i]='\0';
  151. return read[6]; //return error 0=no error
  152. }
  153. /*! \brief Change to transparent mode
  154. * \return bluetooth error if one occur, 0 otherwise
  155. */
  156. char e_bt_tranparent_mode(void)
  157. {
  158. char send[8];
  159. char read[10];
  160. int i;
  161. char c;
  162. send[0]=0x02;
  163. send[1]=0x52;
  164. send[2]=0x11;
  165. send[3]=0x01;
  166. send[4]=0x00;
  167. send[5]=0x64;
  168. send[6]=0x01;//link number 1-30
  169. send[7]=0x03;
  170. e_send_uart1_char(send,8);
  171. i=0;
  172. c=0;
  173. do
  174. {
  175. if (e_getchar_uart1(&read[i])) //read response
  176. {
  177. c=read[i];
  178. i++;
  179. }
  180. }
  181. while (((char)c != 0x03)||(i<(read[3]+6)));
  182. read[i]='\0';
  183. return read[6]; //return error 0=no error
  184. }
  185. /*! \brief Exit from the transparent mode */
  186. void e_bt_exit_tranparent_mode(void)
  187. {
  188. long i;
  189. U1MODEbits.UARTEN=0;//disable uart1
  190. _TRISF3=0;
  191. _LATF3=0;//uart 1 TX
  192. for(i=0;i<MILLISEC;i++);//wait at least 1ms
  193. _LATF3=1;//uart 1 TX
  194. _TRISF3=1;
  195. e_init_uart1();
  196. }
  197. /*! \brief Read the PIN number of this e-puck's bluetooth module
  198. * \param PIN A pointer to store the PIN number
  199. * \return bluetooth error if one occur, 0 otherwise
  200. */
  201. char e_bt_read_local_pin_number(char *PIN)
  202. {
  203. char send[7];
  204. unsigned char read[30];
  205. int i;
  206. char c;
  207. send[0]=0x02; //send PIN request
  208. send[1]=0x52;
  209. send[2]=0x16;
  210. send[3]=0x00;
  211. send[4]=0x00;
  212. send[5]=0x68;
  213. send[6]=0x03;
  214. e_send_uart1_char(send,7);
  215. i=0;
  216. c=0;
  217. do
  218. {
  219. if (e_getchar_uart1(&read[i])) //read response
  220. {
  221. c=read[i];
  222. i++;
  223. }
  224. }
  225. while (((char)c != 0x03)||(i<(read[3]+6)));
  226. read[i]='\0';
  227. for(i=0;i<read[7];i++) //extract PIN
  228. PIN[i]=read[i+8];
  229. PIN[i]= '\0';
  230. return read[6];//return error 0=no error
  231. }
  232. /*! \brief Read the name of this e-puck's bluetooth module
  233. * \param name A pointer to store the name
  234. * \return bluetooth error if one occur, 0 otherwise
  235. */
  236. char e_bt_read_local_name(char *name)
  237. {
  238. char send[7];
  239. char read[40];
  240. int i;
  241. char c;
  242. send[0]=0x02; //send Name request
  243. send[1]=0x52;
  244. send[2]=0x03;
  245. send[3]=0x00;
  246. send[4]=0x00;
  247. send[5]=0x55;
  248. send[6]=0x03;
  249. e_send_uart1_char(send,7);
  250. i=0;
  251. c=0;
  252. do
  253. {
  254. if (e_getchar_uart1(&read[i])) //read response
  255. {
  256. c=read[i];
  257. i++;
  258. }
  259. }
  260. while (((char)c != 0x03)||(i<(read[3]+6)));
  261. read[i]='\0';
  262. for(i=0;i<read[7];i++) //extract Name
  263. name[i]=read[i+8];
  264. return read[6];//return error 0=no error
  265. }
  266. /*! \brief Write the PIN number on this e-puck's bluetooth module
  267. * \param PIN A pointer to store the PIN number
  268. * \return bluetooth error if one occur, 0 otherwise
  269. */
  270. char e_bt_write_local_pin_number(char *PIN)
  271. {
  272. char send[30];
  273. char read[10];
  274. int i;
  275. char c;
  276. int numberlenght;
  277. numberlenght=strlen(PIN);
  278. //send_uart2(PIN,numberlenght);
  279. send[0]=0x02;
  280. send[1]=0x52;
  281. send[2]=0x17;
  282. send[3]=numberlenght+1;
  283. send[4]=0x00;
  284. send[5]=(send[1]+send[2]+send[3]);
  285. send[6]=numberlenght;
  286. for (i=0;i<numberlenght;i++)
  287. send[i+7]=PIN[i];
  288. send[7+numberlenght]=0x03;
  289. e_send_uart1_char(send,numberlenght+8);
  290. i=0;
  291. c=0;
  292. do
  293. {
  294. if (e_getchar_uart1(&read[i])) //read response
  295. {
  296. c=read[i];
  297. i++;
  298. }
  299. }
  300. while (((char)c != 0x03)||(i<(read[3]+6)));
  301. read[i]='\0';
  302. return read[6];//return error 0=no error
  303. }
  304. /*! \brief Write the name on this e-puck's bluetooth module
  305. * \param name A pointer to store the name
  306. * \return bluetooth error if one occur, 0 otherwise
  307. */
  308. char e_bt_write_local_name(char *name)
  309. {
  310. char send[40];
  311. char read[10];
  312. int i;
  313. char c;
  314. int namelenght;
  315. namelenght=strlen(name);
  316. namelenght++;//add null caracter
  317. //send_uart2(PIN,numberlenght);
  318. send[0]=0x02; //send PIN request
  319. send[1]=0x52;
  320. send[2]=0x04;
  321. send[3]=namelenght+1;
  322. send[4]=0x00;
  323. send[5]=(send[1]+send[2]+send[3]);
  324. send[6]=namelenght;
  325. for (i=0;i<namelenght;i++)
  326. send[i+7]=name[i];
  327. send[7+namelenght]=0x03;
  328. e_send_uart1_char(send,namelenght+8);
  329. i=0;
  330. c=0;
  331. do
  332. {
  333. if (e_getchar_uart1(&read[i])) //read response
  334. {
  335. c=read[i];
  336. i++;
  337. }
  338. }
  339. while (((char)c != 0x03)||(i<(read[3]+6)));
  340. read[i]='\0';
  341. return read[6];//return error 0=no error
  342. }
  343. /*! \brief Research all the accessible bluetooth devices
  344. * \param device A pointer to BtDevice to store all the caracteristics
  345. * of each devices found
  346. * \return the number of device found
  347. * \sa BtDevice, e_bt_present_device
  348. */
  349. int e_bt_inquiry(struct BtDevice *device)
  350. {
  351. char send[10];
  352. char read[50];
  353. int devicefound;
  354. int i,j;
  355. char c;
  356. send[0]=0x02; //send Name request
  357. send[1]=0x52;
  358. send[2]=0x00;
  359. send[3]=0x03;
  360. send[4]=0x00;
  361. send[5]=0x55;
  362. send[6]=0x05;//0x01-0x30 time of inquiri in sec
  363. send[7]=0x0A;//number of maximu response 0=infinite
  364. send[8]=0x00;//mode
  365. send[9]=0x03;
  366. e_send_uart1_char(send,10);
  367. devicefound=0;
  368. i=j=0;
  369. c=0;
  370. do{
  371. i=0;
  372. do
  373. {
  374. if (e_getchar_uart1(&read[i])) //read response
  375. {
  376. c=read[i];
  377. i++;
  378. }
  379. }
  380. while (((char)c != 0x03)||(i<(read[3]+6)));
  381. if(read[1]==0x69)
  382. { for(i=0;i<6;i++) //extract BTaddress
  383. device[devicefound].address[i]=read[i+6];
  384. for(i=0;i<3;i++) //extract BTaddress
  385. device[devicefound].class[i]=read[i+12];
  386. devicefound++;
  387. }
  388. }
  389. while((read[1]!=0x43)&(read[2]!=0x00));
  390. return devicefound;//return number of device found
  391. }
  392. /*! \brief To get the friendly name of a bluetooth device
  393. * \param device A pointer on the device that you want the name
  394. * \return bluetooth error if one occur, 0 otherwise
  395. * \sa BtDevice
  396. */
  397. char e_bt_get_friendly_name(struct BtDevice *device)
  398. {
  399. char send[13];
  400. char read[50];
  401. int i;
  402. char c;
  403. send[0]=0x02; //send Name request
  404. send[1]=0x52;
  405. send[2]=0x02;
  406. send[3]=0x06;
  407. send[4]=0x00;
  408. send[5]=0x5a;
  409. send[6]=device[0].address[0];//address of device
  410. send[7]=device[0].address[1];
  411. send[8]=device[0].address[2];
  412. send[9]=device[0].address[3];
  413. send[10]=device[0].address[4];
  414. send[11]=device[0].address[5];
  415. send[12]=0x03;
  416. e_send_uart1_char(send,13);
  417. i=0;
  418. c=0;
  419. do
  420. {
  421. if (e_getchar_uart1(&read[i])) //read response
  422. {
  423. c=read[i];
  424. i++;
  425. }
  426. }
  427. while (((char)c != 0x03)||(i<(read[3]+6)));
  428. read[i]='\0';
  429. if (read[6]==0)
  430. for(i=0;i<read[13];i++) //extract Name
  431. device[0].friendly_name[i]=read[i+14];
  432. return read[6]; //return error 0=no error
  433. }
  434. /*! \brief Try to connect to another bluetooth device
  435. * \param address A pointer on the device address which you want
  436. * to connect
  437. * \return bluetooth error if one occur, 0 otherwise
  438. */
  439. char e_bt_etablish_SPP_link(char *address)
  440. {
  441. char send[15];
  442. char read[50];
  443. int i;
  444. char c;
  445. send[0]=0x02; //send Name request
  446. send[1]=0x52;
  447. send[2]=0x0a;
  448. send[3]=0x08;
  449. send[4]=0x00;
  450. send[5]=0x64;
  451. send[6]=0x01;
  452. send[7]=address[0];//address of device
  453. send[8]=address[1];
  454. send[9]=address[2];
  455. send[10]=address[3];
  456. send[11]=address[4];
  457. send[12]=address[5];
  458. send[13]=0x01;
  459. send[14]=0x03;
  460. e_send_uart1_char(send,15);
  461. i=0;
  462. c=0;
  463. do{
  464. i=0;
  465. do
  466. {
  467. if (e_getchar_uart1(&read[i])) //read response
  468. {
  469. c=read[i];
  470. i++;
  471. }
  472. }
  473. while (((char)c != 0x03)||(i<(read[3]+6)));
  474. }
  475. while((read[2]!=0x69)&(read[2]!=0x0B));//spp link etablished
  476. return read[6]; //return rfcomm error 0=no error
  477. }
  478. /*! \brief Unconnect from the current bluetooth device
  479. * \return bluetooth error if one occur, 0 otherwise
  480. */
  481. char e_bt_release_SPP_link(void)
  482. {
  483. char send[8];
  484. char read[10];
  485. int i;
  486. char c;
  487. send[0]=0x02;
  488. send[1]=0x52;
  489. send[2]=0x0d;
  490. send[3]=0x01;
  491. send[4]=0x00;
  492. send[5]=0x60;
  493. send[6]=0x01;//link number 1-30
  494. send[7]=0x03;
  495. e_send_uart1_char(send,8);
  496. i=0;
  497. c=0;
  498. do
  499. {
  500. if (e_getchar_uart1(&read[i])) //read response
  501. {
  502. c=read[i];
  503. i++;
  504. }
  505. }
  506. while (((char)c != 0x03)||(i<(read[3]+6)));
  507. read[i]='\0';
  508. if((read[2]==0x0d)&(read[6]!=0x00))//control confirm request (in case no link etablished)
  509. return read[6]; //return error 0=no error
  510. do{
  511. i=0;
  512. do
  513. {
  514. if (e_getchar_uart1(&read[i]))//read response
  515. {
  516. c=read[i];
  517. i++;
  518. }
  519. }
  520. while (((char)c != 0x03)||(i<(read[3]+6)));
  521. }
  522. while((read[1]!=0x69)&(read[2]!=0x051));//spp link released
  523. return read[6]; //return rfcomm error 0=no error
  524. }
  525. /*! \brief Send data to the current bluetooth device
  526. * \warning send maximum 127 bytes if you are in non transparent mode
  527. * \param data The datas to send
  528. * \param datalength The length of the datas to send
  529. * \return bluetooth error if one occur, 0 otherwise
  530. */
  531. char e_bt_send_SPP_data(char *data, char datalength)
  532. {
  533. char send[120];
  534. char read[10];
  535. int i;
  536. char c;
  537. //send_uart2(PIN,numberlenght);
  538. send[0]=0x02; //send PIN request
  539. send[1]=0x52;
  540. send[2]=0x0F;
  541. send[3]=datalength+3;
  542. send[4]=0x00;
  543. send[5]=(send[1]+send[2]+send[3]);
  544. send[6]=0x01;//local port
  545. send[7]=datalength;
  546. send[8]=0x00;
  547. for (i=0;i<datalength;i++)
  548. send[i+9]=data[i];
  549. send[9+datalength]=0x03;
  550. e_send_uart1_char(send,datalength+10);
  551. i=0;
  552. c=0;
  553. do
  554. {
  555. if (e_getchar_uart1(&read[i])) //read response
  556. {
  557. c=read[i];
  558. i++;
  559. }
  560. }
  561. while (((char)c != 0x03)||(i<(read[3]+6)));
  562. read[i]='\0';
  563. return read[6];//return error 0=no error
  564. }
  565. /*! \brief Make a list of the bluetooth address of paired device
  566. * \return The number of device found
  567. * \sa e_bt_local_paired_device
  568. */
  569. char e_bt_list_local_paired_device(void)
  570. {
  571. char send[7];
  572. char read[(8*6)+10];
  573. int devicefound;
  574. int i;
  575. char c;
  576. send[0]=0x02;
  577. send[1]=0x52;
  578. send[2]=0x1c;
  579. send[3]=0x00;
  580. send[4]=0x00;
  581. send[5]=0x6e;
  582. send[6]=0x03;
  583. e_send_uart1_char(send,7);
  584. devicefound=0;
  585. i=0;
  586. c=0;
  587. do
  588. {
  589. if (e_getchar_uart1(&read[i])) //read response
  590. {
  591. c=read[i];
  592. i++;
  593. }
  594. }
  595. while (((char)c != 0x03)||(i<(read[3]+6)));
  596. devicefound=read[7];
  597. if(devicefound!=0)
  598. {
  599. for(i=0;i<6*devicefound;i++) //extract BTaddress
  600. e_bt_local_paired_device[i]=read[i+8];
  601. }
  602. return devicefound;//return number of device found
  603. }
  604. /*! \brief Remove a paired bluetooth device
  605. * \param j The number of the paired device to remove from the
  606. * e_bt_local_paired_device array.
  607. * \return bluetooth error if one occur, 0 otherwise
  608. */
  609. char e_bt_remove_local_paired_device(int j)
  610. {
  611. char send[13];
  612. char read[10];
  613. int i;
  614. char c;
  615. send[0]=0x02; //send Name request
  616. send[1]=0x52;
  617. send[2]=0x1b;
  618. send[3]=0x06;
  619. send[4]=0x00;
  620. send[5]=0x73;
  621. send[6]=e_bt_local_paired_device[0+j*6];//address of device
  622. send[7]=e_bt_local_paired_device[1+j*6];
  623. send[8]=e_bt_local_paired_device[2+j*6];
  624. send[9]=e_bt_local_paired_device[3+j*6];
  625. send[10]=e_bt_local_paired_device[4+j*6];
  626. send[11]=e_bt_local_paired_device[5+j*6];
  627. send[12]=0x03;
  628. e_send_uart1_char(send,13);
  629. i=0;
  630. c=0;
  631. do
  632. {
  633. if (e_getchar_uart1(&read[i])) //read response
  634. {
  635. c=read[i];
  636. i++;
  637. }
  638. }
  639. while (((char)c != 0x03)||(i<(read[3]+6)));
  640. read[i]='\0';
  641. return read[6]; //return error 0=no error
  642. }