00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00016
00017
00018
00019
00020
00021 #define DBG_UART0 0
00022
00023 #include "sceptre.h"
00024 #include "btm222.h"
00025 #include <string.h>
00026 #include <stdio.h>
00027
00028 const char btm222_ok_str[] = "OK";
00029 const char btm222_error_str[] = "ERROR";
00030 const char btm222_connect_str[] = "CONNECT";
00031 const char btm222_disconnect_str[] = "DISCONNECT";
00032 char btm222_peer_id[BTM222_MAX_PEER_ID+1] = "<not connected>";
00033
00034 btm222_device_t btm222_devices[BTM_MAX_DEVICES];
00035 int32_t btm222_n_o_devices = 0;
00036 bool_t btm222_connected = false;
00037 char *p_connect = NULL;
00038 char *p_disconnect = NULL;
00039 char *p_peer = NULL;
00040
00041 bool_t btm222_connect_hook(uint32_t ch);
00042
00043 enum
00044 {
00045 connect_hook_idle = 0,
00046 connect_hook_armed,
00047 connect_hook_waiting_for_id,
00048 connect_hook_collecting_id,
00049 };
00050
00051 uint8_t connect_hook_mode = connect_hook_idle;
00052 bool_t connect_hook_reconnect = false;
00053
00054
00055 bool_t btm222_putc(uint8_t ch)
00056 {
00057
00058 uart1_putc(ch,true);
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068 uint32_t c;
00069 if (uart1_getc(&c,100)==false)
00070 {
00071 #if DBG_UART0!=0
00072 uart0_putc('@');
00073 #endif
00074 return false;
00075 }
00076 if ((uint8_t)c!=ch) return false;
00077
00078 return true;
00079 }
00080
00081
00082 bool_t btm222_write(uint8_t *p_data, uint32_t data_size)
00083 {
00084 bool_t result = true;
00085 for (int i=0; i<data_size; i++)
00086 {
00087 if (btm222_putc(p_data[i])==false) result = false;
00088 }
00089 return result;
00090 }
00091
00092
00093 void btm222_write_ex(uint8_t *p_data)
00094 {
00095
00096 btm222_write(p_data,strlen((char*)p_data));
00097
00098
00099 uint32_t last_count = timer_get_count(SYSTEM_TIMER);
00100 while (timer_get_count(SYSTEM_TIMER)-last_count<200)
00101 {
00102 uint32_t ch;
00103 if (uart1_getc(&ch,10)==true)
00104 {
00105 #if DBG_UART0!=0
00106 uart0_putc(ch);
00107 #endif
00108 }
00109 }
00110 }
00111
00112
00113 bool_t btm222_write_command(uint8_t *p_cmd)
00114 {
00115 const char *p_ok = btm222_ok_str;
00116 const char *p_error = btm222_error_str;
00117
00118
00119 btm222_write(p_cmd,strlen((char*)p_cmd));
00120 uint32_t last_count = timer_get_count(SYSTEM_TIMER);
00121
00122
00123 while (*p_ok && *p_error)
00124 {
00125 uint32_t ch;
00126 if (uart1_getc(&ch,10)==true)
00127 {
00128 #if DBG_UART0!=0
00129 uart0_putc(ch);
00130 #endif
00131 if (ch==*p_ok) p_ok++;
00132 else p_ok = btm222_ok_str;
00133 if (ch==*p_error) p_error++;
00134 else p_error = btm222_error_str;
00135 }
00136 if (timer_get_count(SYSTEM_TIMER)-last_count>=200) return false;
00137 }
00138
00139 return *p_ok=='\0'? true : false;
00140 }
00141
00142
00143 bool_t btm222_command(char *p_cmd, char *p_data, uint8_t data_size_max)
00144 {
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155 btm222_write((uint8_t*)"AT",2);
00156 btm222_write((uint8_t*)p_cmd,strlen(p_cmd));
00157 btm222_write((uint8_t*)p_data,MIN(strlen(p_data),data_size_max));
00158 btm222_putc('\r');
00159 return true;
00160 }
00161
00162
00163 uint8_t btm222_seek_devices(void)
00164 {
00165 uint8_t loops;
00166 uint8_t i;
00167 char buffer[64];
00168 char * p;
00169 char * q;
00170
00171 for (i=0; i<BTM_MAX_DEVICES; i++)
00172 {
00173 strcpy(btm222_devices[i].name,"");
00174 strcpy(btm222_devices[i].name,"");
00175 }
00176
00177 btm222_n_o_devices = -1;
00178
00179 if (btm222_command("F?",0,0)==false) return 0;
00180
00181 p = buffer;
00182 *p = '\0';
00183
00184 loops = 30;
00185 uint32_t last_count = timer_get_count(SYSTEM_TIMER);
00186 while (loops && btm222_n_o_devices<8)
00187 {
00188 if (timer_get_count(SYSTEM_TIMER)-last_count>=2000)
00189 {
00190 #if DBG_UART0!=0
00191 uart0_putc('.');
00192 #endif
00193 last_count = timer_get_count(SYSTEM_TIMER);
00194 loops -= 1;
00195 }
00196
00197 uint32_t ch;
00198 if (uart1_getc(&ch,10)==true)
00199 {
00200 #if DBG_UART0!=0
00201 uart0_putc(ch);
00202 #endif
00203 if (ch>=' ')
00204 {
00205
00206 *p++ = ch;
00207 *p = '\0';
00208 }
00209
00210 if (ch=='\r')
00211 {
00212 if (strstr(buffer,"Inquiry Results")) btm222_n_o_devices = 0;
00213 else if (strstr(buffer, "Inquiry End")) loops = 0;
00214 else if (btm222_n_o_devices>=0)
00215 {
00216 p = buffer;
00217 while ((*p) && (*p != '1'+btm222_n_o_devices)) p++;
00218 if (*p)
00219 {
00220 for (i=0; i<3; i++) if (*p) p++;
00221 q = btm222_devices[btm222_n_o_devices].name;
00222 for (i=0; i<18; i++) if (*p) *q++ = *p++;
00223 *q = '\0';
00224 q = btm222_devices[btm222_n_o_devices].id;
00225 for (i=0; i<14; i++) if (*p) *q++ = *p++;
00226 *q = '\0';
00227 btm222_n_o_devices++;
00228 }
00229 }
00230 p = buffer;
00231 *p = '\0';
00232 }
00233 }
00234 }
00235 return btm222_n_o_devices;
00236 }
00237
00238
00239 void btm222_get_parameter(uint8_t param)
00240 {
00241 char str[10];
00242 sprintf(str,"AT%c?\r",param);
00243
00244
00245 btm222_write((uint8_t*)str,strlen(str));
00246
00247
00248 uint32_t last_count = timer_get_count(SYSTEM_TIMER);
00249 while (timer_get_count(SYSTEM_TIMER)-last_count<200)
00250 {
00251 uint32_t ch;
00252 if (uart1_getc(&ch,10)==true)
00253 {
00254
00255 uart0_putc(ch);
00256
00257 }
00258 }
00259 }
00260
00261
00262 void btm222_get_all_parameters(void)
00263 {
00264 btm222_get_parameter('B');
00265 btm222_get_parameter('D');
00266 btm222_get_parameter('E');
00267
00268 btm222_get_parameter('H');
00269 btm222_get_parameter('I');
00270 btm222_get_parameter('K');
00271 btm222_get_parameter('L');
00272 btm222_get_parameter('M');
00273 btm222_get_parameter('N');
00274 btm222_get_parameter('O');
00275 btm222_get_parameter('P');
00276 btm222_get_parameter('Q');
00277 btm222_get_parameter('R');
00278 btm222_get_parameter('Z');
00279 }
00280
00281
00282 void btm222_connect_hook_arm(bool_t reconnect)
00283 {
00284 p_connect = (char *)btm222_connect_str;
00285 p_disconnect = (char *)btm222_disconnect_str;
00286 connect_hook_mode = connect_hook_armed;
00287 connect_hook_reconnect = reconnect;
00288 }
00289
00290
00291 bool_t btm222_connect_hook(uint32_t ch)
00292 {
00293
00294
00295
00296
00297
00298
00299 switch (connect_hook_mode)
00300 {
00301 case connect_hook_idle:
00302
00303 break;
00304
00305 case connect_hook_armed:
00306
00307 if (ch==*p_connect) p_connect++;
00308 else p_connect = (char *)btm222_connect_str;
00309 if (ch==*p_disconnect) p_disconnect++;
00310 else p_disconnect = (char *)btm222_disconnect_str;
00311
00312 if (p_connect-btm222_connect_str>=strlen(btm222_connect_str))
00313 {
00314
00315
00316 connect_hook_mode = connect_hook_waiting_for_id;
00317 p_peer = btm222_peer_id;
00318 }
00319 break;
00320
00321 case connect_hook_waiting_for_id:
00322
00323 if (ch=='\'') connect_hook_mode = connect_hook_collecting_id;
00324 break;
00325
00326 case connect_hook_collecting_id:
00327
00328 if (ch=='\r')
00329 {
00330 if (p_disconnect-btm222_disconnect_str>=strlen(btm222_disconnect_str))
00331 {
00332
00333 btm222_connected = false;
00334
00335 if (connect_hook_reconnect==true) btm222_connect_hook_arm(true);
00336 else connect_hook_mode = connect_hook_idle;
00337 }
00338 else
00339 {
00340
00341 btm222_connected = true;
00342
00343 uart1_flush_buffers();
00344
00345 btm222_connect_hook_arm(connect_hook_reconnect);
00346 return true;
00347 }
00348 }
00349 else if (ch=='\'')
00350 {
00351
00352 }
00353 else if (p_peer-btm222_peer_id<=BTM222_MAX_PEER_ID)
00354 {
00355
00356 *p_peer++ = ch;
00357 *p_peer = '\0';
00358 }
00359 break;
00360 }
00361
00362 return false;
00363 }
00364
00365
00366 bool_t btm222_is_connected(void)
00367 {
00368 return btm222_connected;
00369 }
00370
00371
00372 bool_t btm222_wait_for_connect(uint32_t timeout_ms, bool_t reconnect)
00373 {
00374 btm222_connect_hook_arm(reconnect);
00375 uart1_set_rx_hook(btm222_connect_hook);
00376
00377
00378 uint32_t last_count = timer_get_count(SYSTEM_TIMER);
00379 while (timer_get_count(SYSTEM_TIMER)-last_count<timeout_ms)
00380 {
00381 if (btm222_is_connected()==true) break;
00382 }
00383
00384 if (btm222_is_connected()==true)
00385 {
00386
00387 uart1_flush_buffers();
00388 return true;
00389 }
00390 return false;
00391 }
00392
00393
00394 char *btm222_get_peer_id(void)
00395 {
00396 return btm222_peer_id;
00397 }