Skip to content

Commit 71ca8d1

Browse files
committed
fix bug
1 parent df1d450 commit 71ca8d1

File tree

4 files changed

+285
-300
lines changed

4 files changed

+285
-300
lines changed

class/n21/at_device_n21.c

Lines changed: 71 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636

3737
#ifdef AT_DEVICE_USING_N21
3838

39-
#define N21_WAIT_CONNECT_TIME 30000
39+
#define N21_WAIT_CONNECT_TIME 10000
4040
#define N21_THREAD_STACK_SIZE 2048
4141
#define N21_THREAD_PRIORITY (RT_THREAD_PRIORITY_MAX / 2)
4242
// #define N21_THREAD_PRIORITY 6
@@ -49,8 +49,8 @@ static void n21_power_on(struct at_device *device)
4949
n21 = (struct at_device_n21 *)device->user_data;
5050

5151
/* not nead to set pin configuration for n21 device power on */
52-
#if ( N21_SAMPLE_POWER_PIN != -1)
53-
#if ( N21_SAMPLE_STATUS_PIN != -1)
52+
#if (N21_SAMPLE_POWER_PIN != -1)
53+
#if (N21_SAMPLE_STATUS_PIN != -1)
5454
if (n21->power_pin == -1 || n21->power_status_pin == -1)
5555
{
5656
return;
@@ -66,14 +66,14 @@ static void n21_power_on(struct at_device *device)
6666
rt_thread_mdelay(10);
6767
}
6868
rt_pin_write(n21->power_pin, PIN_LOW);
69-
#else
70-
if( n21->power_pin == -1 )
69+
#else
70+
if (n21->power_pin == -1)
7171
{
7272
return;
7373
}
7474
rt_pin_write(n21->power_pin, PIN_HIGH);
75-
76-
#endif
75+
76+
#endif
7777
#endif
7878
}
7979

@@ -82,9 +82,9 @@ static void n21_power_off(struct at_device *device)
8282
struct at_device_n21 *n21 = RT_NULL;
8383

8484
n21 = (struct at_device_n21 *)device->user_data;
85-
86-
#if ( N21_SAMPLE_POWER_PIN != -1)
87-
#if ( N21_SAMPLE_STATUS_PIN != -1)
85+
86+
#if (N21_SAMPLE_POWER_PIN != -1)
87+
#if (N21_SAMPLE_STATUS_PIN != -1)
8888
/* not nead to set pin configuration for m26 device power on */
8989
if (n21->power_pin == -1 || n21->power_status_pin == -1)
9090
{
@@ -101,13 +101,13 @@ static void n21_power_off(struct at_device *device)
101101
rt_thread_mdelay(10);
102102
}
103103
rt_pin_write(n21->power_pin, PIN_LOW);
104-
#else
105-
if( n21->power_pin == -1 )
104+
#else
105+
if (n21->power_pin == -1)
106106
{
107107
return;
108108
}
109109
rt_pin_write(n21->power_pin, PIN_LOW);
110-
#endif
110+
#endif
111111
#endif
112112
}
113113

@@ -120,11 +120,11 @@ static int n21_netdev_set_info(struct netdev *netdev)
120120
#define N21_INFO_RESP_TIMO rt_tick_from_millisecond(300)
121121

122122
int result = RT_EOK;
123-
// int at_result = 0;
123+
// int at_result = 0;
124124
ip_addr_t addr;
125125
at_response_t resp = RT_NULL;
126126
struct at_device *device = RT_NULL;
127-
127+
128128
if (netdev == RT_NULL)
129129
{
130130
LOG_E("input network interface device is NULL.");
@@ -165,7 +165,7 @@ static int n21_netdev_set_info(struct netdev *netdev)
165165
result = -RT_ERROR;
166166
goto __exit;
167167
}
168-
168+
169169
if (at_resp_parse_line_args_by_kw(resp, "+CGSN:", "+CGSN: %s", iemi) <= 0)
170170
{
171171
LOG_E("n21 device(%s) IEMI get fail", device->name);
@@ -186,10 +186,10 @@ static int n21_netdev_set_info(struct netdev *netdev)
186186
netdev->hwaddr[i] = (iemi[j] - '0');
187187
}
188188
}
189-
190-
LOG_D("hwaddr:%.2X-%.2X-%.2X-%.2X-%.2X-%.2X-%.2X-%.2X,IEMI:%s",netdev->hwaddr[0],
191-
netdev->hwaddr[1],netdev->hwaddr[2],netdev->hwaddr[3],netdev->hwaddr[4],
192-
netdev->hwaddr[5],netdev->hwaddr[6],netdev->hwaddr[7],iemi);
189+
190+
LOG_D("hwaddr:%.2X-%.2X-%.2X-%.2X-%.2X-%.2X-%.2X-%.2X,IEMI:%s", netdev->hwaddr[0],
191+
netdev->hwaddr[1], netdev->hwaddr[2], netdev->hwaddr[3], netdev->hwaddr[4],
192+
netdev->hwaddr[5], netdev->hwaddr[6], netdev->hwaddr[7], iemi);
193193
}
194194
LOG_D("get IP address");
195195
/* get network interface device IP address */
@@ -203,21 +203,21 @@ static int n21_netdev_set_info(struct netdev *netdev)
203203
result = -RT_ERROR;
204204
goto __exit;
205205
}
206-
206+
207207
if (at_resp_parse_line_args_by_kw(resp, "+XIIC: ", "+XIIC:%*[^,],%s", ipaddr) <= 0)
208208
{
209209
LOG_E("n21 device(%s) prase \"AT+XIIC?\" commands resposne data error!", device->name);
210210
result = -RT_ERROR;
211211
goto __exit;
212212
}
213-
213+
214214
LOG_I("n21 device(%s) IP address: %s", device->name, ipaddr);
215215

216216
/* set network interface address information */
217217
inet_aton(ipaddr, &addr);
218218
netdev_low_level_set_ipaddr(netdev, &addr);
219219
}
220-
//n21 不支持设置和查询DNS
220+
//n21 not support inquire dns
221221

222222
__exit:
223223
if (resp)
@@ -238,13 +238,13 @@ static void check_link_status_entry(void *parameter)
238238
at_response_t resp = RT_NULL;
239239
int result_code, link_status;
240240
struct at_device *device = RT_NULL;
241-
#if ( N21_SAMPLE_STATUS_PIN != -1 )
241+
#if (N21_SAMPLE_STATUS_PIN != -1)
242242
struct at_device_n21 *n21 = RT_NULL;
243-
#endif
243+
#endif
244244

245245
char parsed_data[10] = {0};
246246
struct netdev *netdev = (struct netdev *)parameter;
247-
247+
248248
LOG_D("statrt n21 device(%s) link status check \n");
249249

250250
device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name);
@@ -253,9 +253,9 @@ static void check_link_status_entry(void *parameter)
253253
LOG_E("get n21 device by netdev name(%s) failed.", netdev->name);
254254
return;
255255
}
256-
#if ( N21_SAMPLE_STATUS_PIN != -1 )
256+
#if (N21_SAMPLE_STATUS_PIN != -1)
257257
n21 = (struct at_device_n21 *)device->user_data;
258-
#endif
258+
#endif
259259
resp = at_create_resp(N21_LINK_RESP_SIZE, 0, N21_LINK_RESP_TIMO);
260260
if (resp == RT_NULL)
261261
{
@@ -283,26 +283,27 @@ static void check_link_status_entry(void *parameter)
283283
netdev_low_level_set_link_status(netdev, (N21_LINK_STATUS_OK == link_status));
284284
}
285285

286-
#if ( N21_SAMPLE_STATUS_PIN != -1 )
286+
#if (N21_SAMPLE_STATUS_PIN != -1)
287287
if (rt_pin_read(n21->power_status_pin) == PIN_HIGH) //check the module_status , if moduble_status is Low, user can do your logic here
288288
{
289-
#endif
289+
#endif
290290
if (at_obj_exec_cmd(device->client, resp, "AT+CSQ") == 0)
291291
{
292292
at_resp_parse_line_args_by_kw(resp, "+CSQ:", "+CSQ: %s", &parsed_data);
293293
if (strncmp(parsed_data, "99,99", sizeof(parsed_data)))
294294
{
295-
LOG_D("n21 device(%s) signal strength: %s", device->name, parsed_data);
295+
LOG_W("n21 device(%s) signal strength: %s", device->name, parsed_data);
296296
}
297297
}
298-
#if ( N21_SAMPLE_STATUS_PIN != -1 )
298+
#if (N21_SAMPLE_STATUS_PIN != -1)
299299
}
300300
else
301301
{
302-
//LTE down
303-
LOG_E("the lte pin is low");
302+
LOG_E("netdev name(%s) status pin is low", device->name);
303+
netdev_low_level_set_link_status(netdev, RT_FALSE);
304+
return;
304305
}
305-
#endif
306+
#endif
306307
rt_thread_mdelay(N21_LINK_DELAY_TIME);
307308
}
308309
}
@@ -385,7 +386,7 @@ static int n21_netdev_set_down(struct netdev *netdev)
385386

386387
#ifdef NETDEV_USING_PING
387388
static int n21_netdev_ping(struct netdev *netdev, const char *host,
388-
size_t data_len, uint32_t timeout, struct netdev_ping_resp *ping_resp)
389+
size_t data_len, uint32_t timeout, struct netdev_ping_resp *ping_resp)
389390
{
390391
#define N21_PING_RESP_SIZE 512
391392
#define N21_PING_IP_SIZE 16
@@ -423,7 +424,7 @@ static int n21_netdev_ping(struct netdev *netdev, const char *host,
423424
}
424425

425426
/* send "AT+PING=<ip>[,<timeout>,<size>,<num>]" commond to send ping request */
426-
/* n21 ping命令 size 取值范围 ipv4(36-1500) ipv6(56-1500) */
427+
/* n21 ping <size> ranges of ipv4(36-1500), ipv6(56-1500) */
427428
if (at_obj_exec_cmd(device->client, resp, "AT+PING=%s,%d,%d,1",
428429
host, N21_PING_TIMEO / (RT_TICK_PER_SECOND / 10), data_len + 4) < 0)
429430
{
@@ -474,7 +475,7 @@ const struct netdev_ops n21_netdev_ops =
474475
n21_netdev_set_down,
475476

476477
RT_NULL, /* not support set ip, netmask, gatway address */
477-
RT_NULL,/* not support set dns server */
478+
RT_NULL, /* not support set dns server */
478479
RT_NULL, /* not support set DHCP status */
479480

480481
#ifdef NETDEV_USING_PING
@@ -534,7 +535,7 @@ static void n21_init_thread_entry(void *parameter)
534535
#define CSQ_RETRY 10
535536
#define CREG_RETRY 10
536537
#define CEREG_RETRY 30
537-
#define CCID_SIZE 20
538+
#define CCID_SIZE 20
538539

539540
int i, retry_num = INIT_RETRY;
540541
char ccid[CCID_SIZE] = {0};
@@ -554,7 +555,7 @@ static void n21_init_thread_entry(void *parameter)
554555
while (retry_num--)
555556
{
556557
rt_memset(parsed_data, 0, sizeof(parsed_data));
557-
// rt_thread_mdelay(1000);
558+
558559
n21_power_on(device);
559560
rt_thread_mdelay(5000); //check the n21 hardware manual, when we use the pow_key to start n21, it takes about 20s,so we put 25s here to ensure starting n21 normally.
560561

@@ -578,18 +579,18 @@ static void n21_init_thread_entry(void *parameter)
578579
/* check SIM card */
579580
for (i = 0; i < CPIN_RETRY; i++)
580581
{
581-
at_resp_set_info(resp, 128, 2, 5 * RT_TICK_PER_SECOND);
582-
if (at_obj_exec_cmd(client, resp, "AT+CCID") < 0)
583-
{
584-
LOG_E("AT+CCID ERROR! retry:%d.",i);
582+
at_resp_set_info(resp, 128, 2, 5 * RT_TICK_PER_SECOND);
583+
if (at_obj_exec_cmd(client, resp, "AT+CCID") < 0)
584+
{
585+
LOG_E("AT+CCID ERROR! retry:%d.", i);
585586
rt_thread_mdelay(1000);
586-
continue;
587-
}
587+
continue;
588+
}
588589

589-
if (at_resp_parse_line_args_by_kw(resp, "+CCID:", "+CCID: %s",ccid))
590+
if (at_resp_parse_line_args_by_kw(resp, "+CCID:", "+CCID: %s", ccid))
590591
{
591592
LOG_I("n21 device(%s) SIM card detection success.", device->name);
592-
LOG_I("CCID: %s",ccid);
593+
LOG_I("CCID: %s", ccid);
593594
break;
594595
}
595596
rt_thread_mdelay(1000);
@@ -600,7 +601,7 @@ static void n21_init_thread_entry(void *parameter)
600601
result = -RT_ERROR;
601602
goto __exit;
602603
}
603-
604+
604605
/* waiting for dirty data to be digested */
605606
rt_thread_mdelay(10);
606607
LOG_I("register network!");
@@ -681,9 +682,10 @@ static void n21_init_thread_entry(void *parameter)
681682
{
682683
/* "CT" */
683684
LOG_I("n21 device(%s) network operator: %s", device->name, parsed_data);
684-
}else
685+
}
686+
else
685687
{
686-
LOG_E("n21 device(%s) Unknown carrier:%s",device->name, parsed_data);
688+
LOG_E("n21 device(%s) Unknown carrier:%s", device->name, parsed_data);
687689
}
688690
/* the device default response timeout is 150 seconds, but it set to 20 seconds is convenient to use. */
689691
AT_SEND_CMD(client, resp, 0, 20 * 1000, "AT+XIIC=1");
@@ -696,9 +698,9 @@ static void n21_init_thread_entry(void *parameter)
696698
goto __exit;
697699
}
698700
result = RT_EOK;
699-
700-
AT_SEND_CMD(client, resp, 0, 300, "AT+RECVMODE=1"); //设置直接接收
701-
701+
702+
AT_SEND_CMD(client, resp, 0, 300, "AT+RECVMODE=1"); //set direct receive
703+
702704
__exit:
703705
if (result == RT_EOK)
704706
{
@@ -722,8 +724,12 @@ static void n21_init_thread_entry(void *parameter)
722724
if (result == RT_EOK)
723725
{
724726
/* set network interface device status and address information */
725-
n21_netdev_set_info(device->netdev);
726-
n21_netdev_check_link_status(device->netdev);
727+
n21_netdev_set_info(device->netdev);
728+
/* check and create link staus sync thread */
729+
if (rt_thread_find(device->netdev->name) == RT_NULL)
730+
{
731+
n21_netdev_check_link_status(device->netdev);
732+
}
727733
LOG_I("n21 device(%s) network initialize success!", device->name);
728734
}
729735
else
@@ -763,7 +769,7 @@ static void urc_func(struct at_client *client, const char *data, rt_size_t size)
763769

764770
/* n21 device URC table for the device control */
765771
static const struct at_urc urc_table[] =
766-
{
772+
{
767773
{"+PBREADY", "\r\n", urc_func},
768774
{"CLOSED", "\r\n", urc_func},
769775
};
@@ -823,15 +829,14 @@ static int n21_reset(struct at_device *device)
823829

824830
/* n21 only poweroff cmd,not support reboot.*/
825831
/* use power pin reboot */
826-
//result = at_obj_exec_cmd(client, RT_NULL, "AT+RESET");
827-
#if (N21_SAMPLE_POWER_PIN != -1)
828-
rt_pin_write(N21_SAMPLE_POWER_PIN,0);
832+
#if (N21_SAMPLE_POWER_PIN != -1)
833+
rt_pin_write(N21_SAMPLE_POWER_PIN, 0);
829834
rt_thread_mdelay(500);
830-
rt_pin_write(N21_SAMPLE_POWER_PIN,1);
831-
835+
rt_pin_write(N21_SAMPLE_POWER_PIN, 1);
836+
832837
rt_thread_mdelay(1000);
833838

834-
/* waiting 10 seconds for mw31 device reset */
839+
/* waiting 10 seconds for n21 device reset */
835840
device->is_init = RT_FALSE;
836841
if (at_client_obj_wait_connect(client, N21_WAIT_CONNECT_TIME))
837842
{
@@ -842,10 +847,10 @@ static int n21_reset(struct at_device *device)
842847
n21_net_init(device);
843848

844849
device->is_init = RT_TRUE;
845-
#else
850+
#else
846851
result = -RT_ERROR;
847-
#endif
848-
852+
#endif
853+
849854
return result;
850855
}
851856

0 commit comments

Comments
 (0)