10 static volatile uint16_t buffer_index=0;
11 static volatile uint8_t resp_buffer_overflow=0;
12 static volatile uint8_t resp_buffer_overrun=0;
13 static volatile uint8_t lock_buffer=0;
14 #define NO_RESP_RECEIVED 0
16 #define RECEIVED_ERROR 2
17 static volatile uint8_t resp_received=NO_RESP_RECEIVED;
21 static const uint8_t resp_end_OK[] =
"OK";
22 static const uint8_t resp_end_ERR[] =
"ERROR";
23 static volatile uint8_t OK_index=0;
24 static volatile uint8_t ERR_index=0;
26 #if USE_ADVANCED_URC_SEARCH == 1
27 #define UDP_URC_LIST_INDEX 0
28 #define NIDD_URC_LIST_INDEX 1
36 #define URC_UDPRCV_LEN 9
37 static const uint8_t resp_urc_udp_rcv[] =
"+UUSORF: ";
38 static volatile uint8_t URC_index=0;
39 static volatile uint8_t URC_UDP_received=0;
42 void USART3_IRQHandler(
void)
46 volatile uint32_t SR_content;
47 #if USE_ADVANCED_URC_SEARCH == 1
51 SR_content = USART3->SR;
57 if (SR_content & USART_SR_ORE)
63 #if USE_ADVANCED_URC_SEARCH == 1
75 response_buffer[buffer_index] = tmp;
78 #if USE_ADVANCED_URC_SEARCH == 1
82 if (URC_search_list[k].URC_detected == 0)
85 if (URC_search_list[k].URC_header_detected == 0)
88 if (response_buffer[buffer_index] == URC_search_list[k].URC_string_name[URC_search_list[k].match_counter])
93 if (URC_search_list[k].URC_string_name[URC_search_list[k].match_counter] == 0)
108 if ((response_buffer[buffer_index] == 0x0A) && (response_buffer[buffer_index - 1] == 0x0D))
119 #warning Firmware compiled with legacy URC search engine. NIDD will not work properly.
120 if (response_buffer[buffer_index]==resp_urc_udp_rcv[URC_index])
124 if (URC_index>=URC_UDPRCV_LEN)
135 if ((urc_header_found) && (response_buffer[buffer_index]==0x0A) && (response_buffer[buffer_index-1]==0x0D))
144 #warning URC support turned off. UDP and NIDD will not work properly.
147 #ifndef DEBUG_NO_RESP_END_SEARCH
148 if (response_buffer[buffer_index]==resp_end_OK[OK_index])
152 if (OK_index>=OK_LEN)
156 resp_received = RECEIVED_OK;
164 if (response_buffer[buffer_index]==resp_end_ERR[ERR_index])
168 if (ERR_index>=ERR_LEN)
172 resp_received = RECEIVED_ERROR;
180 #warning Response end detection is deactivated; AT-command engine is in debug mode.
192 if ((tmp!=
'\r') && (tmp!=
'\n') && (tmp!=
' '))
194 resp_buffer_overrun=1;
201 resp_buffer_overflow=1;
206 uint8_t __wait_AT_response(uint32_t timeout)
208 volatile uint32_t safety_counter;
211 while ((resp_received==NO_RESP_RECEIVED) && (safety_counter<timeout))
216 return resp_received;
220 void __AT_FSM_rearm(
void)
222 #if USE_ADVANCED_URC_SEARCH == 1
226 resp_received = NO_RESP_RECEIVED;
228 resp_buffer_overflow = 0;
229 resp_buffer_overrun = 0;
231 #if USE_ADVANCED_URC_SEARCH == 1
239 URC_UDP_received = 0;
243 uint16_t
AT_ReadReponseBuffer(uint8_t* buffer_out,uint16_t buffer_length,uint8_t* is_overflowed,uint8_t* overrun_detected,uint8_t *error_status,uint32_t max_wait_time)
246 uint16_t data_length;
249 #ifndef NO_RESP_END_SEARCH
250 wait_result = __wait_AT_response(max_wait_time);
252 if (wait_result==NO_RESP_RECEIVED)
255 if (is_overflowed!=NULL)
257 (*is_overflowed) = 0;
260 if (overrun_detected!=NULL)
262 (*overrun_detected) = 0;
274 data_length = buffer_index;
276 if (buffer_out!=NULL)
278 for (k=0; k<data_length; k++)
280 if (k==(buffer_length-1))
285 buffer_out[k] = response_buffer[k];
291 if (is_overflowed!=NULL)
293 (*is_overflowed) = resp_buffer_overflow;
296 if (overrun_detected!=NULL)
298 (*overrun_detected) = resp_buffer_overrun;
301 if (error_status!=NULL)
303 if (wait_result==RECEIVED_ERROR)
332 resp = __wait_AT_response(max_wait_time);
334 if ((resp==NO_RESP_RECEIVED) || (resp==RECEIVED_ERROR))
336 if (resp==RECEIVED_ERROR)
345 response_buffer[buffer_index]=0;
347 data_start = strstr((
char *)response_buffer,
"+USOCR: ");
349 if (data_start==NULL)
358 socket_id = data_start[8] -
'0';
370 snprintf(cmd,20,
"AT+USOCL=%d\r\n",socket_id);
373 result = __wait_AT_response(max_wait_time);
375 if ((result == NO_RESP_RECEIVED) || (result == RECEIVED_ERROR))
377 if (result==RECEIVED_ERROR)
390 at_udp_error_t AT_SendUDPData(uint8_t socket_id,uint8_t *target_IP_string,uint16_t target_port,uint8_t *data,uint16_t data_length,uint32_t max_wait_time)
392 uint8_t cmd_header[30];
393 const uint8_t conv_LUT[] = {
'0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
'A',
'B',
'C',
'D',
'E',
'F'};
399 resp = __wait_AT_response(max_wait_time);
401 if (resp==NO_RESP_RECEIVED)
408 if (resp==RECEIVED_ERROR)
413 sprintf(cmd_header,
"AT+USOST=%d,\"%s\",%d,%d,\"",socket_id,target_IP_string,target_port,data_length);
416 for (k=0; k<data_length; k++)
424 resp = __wait_AT_response(max_wait_time);
426 if (resp==NO_RESP_RECEIVED)
433 if (resp==RECEIVED_ERROR)
445 #if USE_ADVANCED_URC_SEARCH == 1
446 if (URC_search_list[UDP_URC_LIST_INDEX].URC_detected == 0)
448 if (URC_UDP_received==0)
456 if (packet_length!=NULL)
458 (*packet_length) = 0;
464 response_buffer[buffer_index] = 0;
466 data_start = strstr((
char *)response_buffer,
"+UUSORF: ");
471 (*socket_id) = data_start[9] -
'0';
475 if (packet_length!=NULL)
478 (*packet_length) = 0;
479 while ((data_start[11+k]>=
'0') && (data_start[11+k]<=
'9') && (k<5))
481 (*packet_length) *= 10;
482 (*packet_length) += data_start[11 + k] -
'0';
500 #if USE_ADVANCED_URC_SEARCH == 0
501 #warning AT_CheckNIDDReceived() is unavailable because advanced URC search is not included. Placeholder code is compiled instead of AT_CheckNIDDReceived().
504 uint32_t URC_data_length;
505 uint8_t URC_data_buffer[520];
508 if (URC_search_list[NIDD_URC_LIST_INDEX].URC_detected == 0)
513 response_buffer[buffer_index] = 0;
515 sscanf((
char *)response_buffer,
"\r\n\r\n+CRTDCP:%*d,%d,\"%[^\r\n\"]",&URC_data_length,URC_data_buffer);
517 if (data_out != NULL)
520 while ((k<URC_data_length) && (k<512))
522 data_out[k] = URC_data_buffer[k];
531 return URC_data_length;
535 inline uint8_t h_digit_to_nibble(uint8_t h_digit)
537 if ((h_digit>=
'0') && (h_digit<=
'9'))
539 return h_digit -
'0';
542 if ((h_digit>=
'A') && (h_digit<=
'F'))
544 return (h_digit -
'A') + 10;
547 if ((h_digit>=
'a') && (h_digit<=
'f'))
549 return (h_digit -
'a') + 10;
555 at_udp_error_t AT_ReadUDPData(uint8_t socket_id,uint16_t read_length,uint8_t *source_IP,uint16_t *source_port,uint8_t *data_out,uint32_t max_wait_time)
559 uint8_t *response_pointer;
560 uint8_t ip_string[20];
562 uint16_t data_bytes_count;
563 uint8_t data_string[128];
567 sprintf(cmd,
"AT+USORF=%d,%d\r\n",socket_id,read_length);
570 resp = __wait_AT_response(max_wait_time);
572 if (resp == NO_RESP_RECEIVED)
577 if (resp == RECEIVED_ERROR)
584 response_buffer[buffer_index] = 0;
586 response_pointer = strstr((
char *)response_buffer,
"+USORF: ");
588 if (sscanf(response_pointer,
"+USORF: %*d,\"%[^\"]\",%d,%d,\"%[^\"]",ip_string,&port,&data_bytes_count,data_string) != 4)
600 while (k<(data_bytes_count*2))
602 data_out[data_ptr] = (h_digit_to_nibble(data_string[k]) << 4) | h_digit_to_nibble(data_string[k+1]);
610 strcpy(source_IP,ip_string);
613 if (source_port!=NULL)
615 (*source_port) = port;
629 uint8_t buf_copy[100];
630 uint16_t buf_copy_length;
637 if ((error!=0) || (buf_copy_length==0))
642 result_pos = strstr((
char *)buf_copy,
"+CSQ: ");
644 if (result_pos==NULL)
656 while ((result_pos[k]!=
',') && (result_pos[k]!=0) && (k<8))
659 rssi_num += (result_pos[k] -
'0');
663 if ((rssi_num>=2) && (rssi_num<=30))
665 rssi_actual = 2*rssi_num - 113;
687 uint8_t buf_copy[100];
688 uint16_t buf_copy_length;
695 if ((error!=0) || (buf_copy_length==0))
705 id_pos = strstr((
char *)buf_copy,
"+CCID: ");
715 while ((id_pos[7+k]!=
'\r') && (k<49))
717 ICCID[k] = id_pos[7+k];
729 uint8_t buf_copy[100];
731 uint16_t buf_copy_length;
737 if ((error!=0) || (buf_copy_length==0))
747 resp_start = strstr(buf_copy,
"AT+CGSN\r\r\n");
752 while ((resp_start[10+k]!=
'\r') && (k<49))
754 IMEI[k] = resp_start[10+k];
765 uint8_t cmd_buffer[512];
766 uint16_t string_length;
771 while ((str[string_length]!=0) && (string_length<450))
776 if (string_length==450)
782 snprintf(cmd_buffer,512,
"AT+CSODCP=1,%d,\"%s\"\r\n",string_length,str);
808 resp_ptr = strstr(resp,
"+CEREG:");
810 if (resp_ptr == NULL)
816 res = sscanf(resp_ptr,
"+CEREG: %*d,%d",®_state);
818 if ((reg_state != 1) || (res == 0))