blob: 1e6ad27166e03071b9486c85b71dabee1c1ccfaa [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001//#include "at_com.h"
2#include <stdio.h>
3#include <stdlib.h>
4#include <string.h>
5#include <ctype.h>
6#include <errno.h>
7#include <sys/types.h>
8#include <sys/wait.h>
9#include <sys/msg.h>
10#include "at_api.h"
11
12#define AT_PARAM_MAX_NUM 50
13#define RESEND_TIMES_MAX 3
14
15//µ÷Óøýӿڷ¢ËÍÖ÷¶¯Éϱ¨×Ö·û´®¸øÔ¶¶ËоƬ
16int send_rsp_str_to_modem(int module_id, char *rsp_cmd,int rsp_len)
17{
18 return at_send_msg(module_id, MODULE_ID_AT_CLIENT, MSG_CMD_SEND_RSP_TO_ZTE, rsp_len, rsp_cmd, 0);
19}
20
21//»ñÈ¡ATÃüÁîǰ׺
22static void get_at_req_prefix(char *at_str, char** at_prefix)
23{
24 char *prefix = NULL;
25 char *pszAtHead = NULL;
26 char *ptemstr = NULL;
27
28 if((*at_str == 'A'||*at_str == 'a')&&(*(at_str+1) == 'T'||*(at_str+1) == 't'))
29 {
30 if(isalpha(*(at_str+2)))
31 {
32 *at_prefix = NULL;
33 }
34 else
35 {
36 prefix = (char*)malloc(50);
37 if(prefix == NULL) at_assert(0);
38 memset(prefix, 0x00, 50);
39
40 pszAtHead = at_str;
41 //ÕÒµ½Ê׸ö·Ç×Öĸ»òÕßÊý×ֵIJÎÊý£¬ÀýÈç+
42 for(; *at_str != '\0'; at_str++)
43 {
44 if(!(isalnum(*at_str) || *at_str == '\r' || *at_str == '\n'))
45 {
46 pszAtHead = at_str;
47 pszAtHead++;
48 break;
49 }
50 }
51
52 for(ptemstr = pszAtHead; *ptemstr != '\0'; ptemstr++)
53 {
54 if(*ptemstr == '=' || *ptemstr == '?' || *ptemstr == '\r')
55 {
56 memcpy(prefix, pszAtHead, ptemstr-pszAtHead);
57 *at_prefix = prefix;
58 return;
59 }
60 }
61 }
62 }
63 else
64 {
65 at_assert(0);
66 }
67
68}
69
70static int is_print_str(const char *str){
71 while(isprint(*str)){
72 str++;
73 }
74 return *str == '\0';
75}
76
77static int get_fmt_param_count(const char *str){
78 int fmt_param_count = 0;
79
80 for(; *str != '\0'; str++){
81 if(*str == ','){
82 fmt_param_count++;
83 }
84 }
85 return fmt_param_count + 1;
86}
87
88
89
90/*return 0±íʾ½âÎö²ÎÊýÕý³££¬ÄÚ²¿½øÐÐÑϸñµÄ²ÎÊý¼ì²é£¬°üÀ¨²ÎÊý¸öÊýÒ»ÖÂÐÔ¼ì²é£¬¶ÔÓÚ¿ÉÑ¡²ÎÊý£¬ÐèÒªµ÷ÓÃÕß×ÔÐп¼ÂÇ*/
91static int parse_param_safe(char *fmt, char *buf, void **pval)
92{
93 char *p;
94 char *sepp;
95 char *tmp;
96 char sepc;
97 char *pstr[AT_PARAM_MAX_NUM] = {0};
98 int len, flag;
99 int n = 0;
100 int nparam = 0;//buf×Ö·û´®Êµ¼ÊµÄ²ÎÊý¸öÊý
101 int fmt_param_num = 0;//fmt¸ñʽ»¯ÖÐÒªÇóµÄ²ÎÊý×ܸöÊý£¬Óë%¸öÊýÒ»ÖÂ
102 int ret = AT_PARSE_OK;
103 int mark_flag = 0;//ÓÃÓÚ±êÊ¶ÌØÊâ·ûºÅµÄÅä¶ÔÇé¿ö£¬Ä¿Ç°ÓÃÓÚË«ÒýºÅ
104
105 while(*buf == ' ') buf++;
106 len = strlen(buf) + 1;
107 tmp = p = (char*)malloc(len);
108 if (p == NULL) {
109 return ATERR_NO_PRINT;
110 }
111 //fmt_param_num = find_char_num(fmt,"%");
112 memset(p, 0, len);
113 //ÉêÇëÐÂÄÚ´æ´æ·ÅÈë²ÎµÄATÃüÁÒÔ±ã½øÐÐÕûÐζ¨ÖÆ
114 memcpy(p, buf, strlen(buf));
115 /*È¥³ýβ²¿µÄ\r\n£¬Ä¿Ç°½öÔÚ7100ÉÏÓдËÎÊÌâ */
116 for(len--; len > 0; len--){
117 if(p[len - 1] == '\r' || p[len - 1] == '\n'){
118 p[len - 1] = '\0';
119 } else {
120 break;
121 }
122 }
123 if(!is_print_str(p)){/* ÈçATÃüÁîÖаüº¬²»¿É¼û×Ö·ûÖ±½Ó·µ»Ø´íÎó */
124 free(tmp);
125 return ATERR_NO_PRINT;
126 }
127 /*°´¶ººÅ½øÐÐÿ¸ö²ÎÊýµÄ·Ö¸îÕûÐÎ,Ë«ÒýºÅÀïµÄ¶ººÅÌø¹ý*/
128 do {
129 flag = 0;
130 mark_flag = 0;
131 if (*p == '"') {
132 int i, j;
133 for (i = j = 0, ++p; p[j] != '\0'; i++, j++) {
134 if (p[j] == '"') {
135 ++j;
136 mark_flag = 1;
137 break;
138 }
139 }
140 if(mark_flag == 0){/* δƥÅ䵽˫ÒýºÅÖ±½Ó·µ»Ø´íÎó */
141 free(tmp);
142 return ATERR_DROP_MARK;
143 }
144 if (j == 1) flag = 1;
145 p[j - 1] = '\0';
146 sepp = p + j;
147 } else {
148 sepp = p + strcspn(p, ",");
149 if (p == sepp) flag = 1;
150 }
151
152 nparam++;
153 sepc = sepp[0];
154 sepp[0] = '\0';
155
156 if (flag == 1)
157 pstr[n++] = NULL;
158 else
159 pstr[n++] = p;
160 p = sepp + 1;
161 } while ((n < AT_PARAM_MAX_NUM) && (sepc == ','));
162
163 if(nparam < get_fmt_param_count(fmt)){
164 ret = ATWARN_DROP_RN;
165 }else if(nparam > get_fmt_param_count(fmt)){
166 ret = ATWARN_LACK_PARAM;
167 }
168 /* convert the string params to the types that fmt Appointed */
169 int param_count = 0; /*ÊäÈëµÄ×Ö·û´®ÖÐÓÐЧ²ÎÊý¸öÊý*/
170 int param_str_index = 0; /* the index of string params */
171 int param_val_index = 0; /* the index of param values */
172 //°´ÕÕÈë²Î¸ñʽ½øÐÐÿ¸ö²ÎÊýÖµµÄ·µ»Ø¸³Öµ
173 for (; fmt != NULL && param_str_index < nparam; param_str_index++) {
174 char type;
175 unsigned int size;
176 char str_param_size[10] = { 0 };
177 int param_size_index = 0;
178
179 while(*fmt == ' ') fmt++;
180 if(*fmt == ','){
181 fmt++;
182 continue; /* igmore the param that is without type */
183 }
184 if(*fmt++ != '%'){
185 ret = ATERR_NO_PRINT;
186 break;
187 }
188 for (; param_size_index < sizeof(str_param_size) - 1; fmt++, param_size_index++){
189 if(!isdigit(*fmt)){
190 break;
191 }
192 str_param_size[param_size_index] = *fmt;
193 }
194 type = *(fmt++);
195 size = atoi(str_param_size);
196 while(*fmt == ' ') fmt++; /* igmore the blanks */
197 fmt = (*fmt == ',' ? fmt + 1 : NULL);
198
199 if (type == 'd') {
200 if(size == 0 || size == 4) {
201 if(pstr[param_str_index] != NULL) {
202 *((int *)pval[param_val_index]) = (int)atoi(pstr[param_str_index]);
203 param_count++;
204 }
205 } else if(size == 1) {
206 if(pstr[param_str_index] != NULL) {
207 *((char *)pval[param_val_index]) = (char)atoi(pstr[param_str_index]);
208 param_count++;
209 }
210 } else if(size == 2) {
211 if(pstr[param_str_index] != NULL) {
212 *((short *)pval[param_val_index]) = (short)atoi(pstr[param_str_index]);
213 param_count++;
214 }
215 }else {
216 at_assert(0);
217 break;
218 }
219 } else if (type == 's') {
220 if (size == 0) {
221 if(pstr[param_str_index] != NULL) {
222 strcpy((char *)pval[param_val_index], pstr[param_str_index]);
223 param_count++;
224 }
225 } else {
226 if(size < strlen(pstr[param_str_index])){
227 ret = ATERR_STR_TOO_LONG;
228 break;
229 }
230 if(pstr[param_str_index] != NULL) {
231 strncpy((char *)pval[param_val_index], pstr[param_str_index], size - 1);
232 param_count++;
233 }
234 }
235 } else {
236 at_assert(0);
237 break;
238 }
239 param_val_index++;
240 }
241 free(tmp);
242 return ret;
243}
244
245
246/*%d¸ñʽʱ£¬Èë²Î±ØÐëΪintÀàÐÍ£¬·ñÔòÄÚ²¿ÓÐÔ½½çµÄ·çÏÕ*/
247/*Óû§Ê¹ÓÃʱ£¬Èô²»ÏëÓɸýӿÚÄÚ²¿½øÐвÎÊýµÄ¸ñʽ»¯½âÎö£¬¿ÉÒÔ½«fmt¸³ÖµÎª"%s"£¬pval¸³ÖµÎªchar **p˫ָÕ룬ÕâÑù²ÎÊý½«×÷ΪÕûÌå×Ö·û´®·µ»Ø¸øµ÷ÓÃÕß*/
248int parse_param(char *fmt, char *buf, void **pval)
249{
250 int ret = parse_param_safe(fmt,buf,pval);
251 if(ret==ATWARN_LACK_PARAM||ret==ATWARN_DROP_RN)
252 return AT_PARSE_OK;
253 return ret;
254}
255
256
257//¸øat_client·¢ËÍÇëÇóÏûÏ¢£¬Ð¯´øatÃüÁî×Ö·û´®ºÍ³¬Ê±Ê±³¤
258static void send_app_req(int module_id, char *req_at, int len, int timeout,int direct)
259{
260 struct app_req appreq;
261
262 memset(&appreq, 0x00, sizeof(struct app_req));
263 memcpy(appreq.atstr, req_at, len);
264 appreq.str_len = len;
265 appreq.timeout = timeout;
266 if(direct == NEAR_PS)
267 at_send_msg(module_id, MODULE_ID_AT_CLIENT, MSG_CMD_SEND_AT_TO_ZTE,sizeof(struct app_req), &appreq, 0);
268
269 printf("send_app_req\n");
270
271}
272
273
274//·µ»Ø1 ¿É³¢ÊÔÖØ·¢ ·µ»Ø0 ²»ÔÙÖØ·¢
275int at_client_err_proc(int err, int sent_cnt)
276{
277 if(9001 == err)//ÖÐÐË΢·µ»Ø×´Ì¬»úÒì³££¬ÐèÒªÕû»úÖØÆô
278 {
279 at_assert(0);
280 }
281 if(6003 == err || 6004 == err || 3 == err || 4 == err)
282 {
283 if(sent_cnt == 1)
284 {
285 return 1;
286 }
287 else
288 {
289 printf("please check req\n");
290 }
291 }
292 else if(8008 == err || 6000 == err)
293 {
294 if(sent_cnt == 1)
295 {
296 return 1;
297 }
298 else
299 {//Á¬ÐøÁ½´Î³¬Ê±»òͨµÀ×èÈû£¬ÖÐÐË΢оƬÒì³££¬ÐèÒªÕû»úÖØÆô
300 at_assert(0);
301 }
302 }
303
304 return 0;
305}
306
307
308/**
309 * @brief Ó¦Ó÷¢ËͶ¯×÷ÀàATÇëÇ󣬲¢×èÈûµÈ´ý¶¯×÷½á¹û£»×¢Ò⣺¸Ã½Ó¿ÚÖ»ÄÜÓÃÓÚ²éѯºÍÉèÖúÍÖ´ÐÐÇëÇó£¬Í¨¹ýÈë²Î½øÐÐÖмä½á¹ûµÄÄÚ²¿½âÎö´¦Àí£¬²¢ÇÒ
310 ÓÐÖмä½á¹ûÉϱ¨Ê±£¬Ö»Ö§³ÖÒ»ÌõÖмä½á¹ûµÄÉϱ¨½âÎö£»
311 * @param req_at ÇëÇóATÃüÁî
312 * @param info_fmt ATÏìÓ¦µÄ¸ñʽ»¯·½Ê½
313 * @param pval ¸ñʽ»¯ºóµÄATÏìÓ¦
314 * @param safe_parm_check ָʾÊÇ·ñ¶Ô²ÎÊý¸öÊý½øÐÐÒ»ÖÂÐÔ¼ì²é£¬0±íʾº¬¿ÉÑ¡²ÎÊý£¬²»¶Ô²ÎÊý¸öÊý½øÐмì²é;1±íʾ¶Ô²ÎÊý¸öÊý½øÐÐÑϸñ¼ì²é
315 * @return 0±íʾ·µ»Ø³É¹¦£¬ÆäËûÖµ±íʾʧ°Ü´íÎóÂë
316 * @note 1. Óû§Ê¹ÓÃʱ£¬Èô²»ÏëÓɸýӿÚÄÚ²¿½øÐвÎÊýµÄ¸ñʽ»¯½âÎö£¬¿ÉÒÔ½«info_fmt¸³ÖµÎª"%s"£¬pval¸³ÖµÎªchar **p˫ָÕ룬ÕâÑù²ÎÊý½«×÷ΪÕûÌå×Ö·û´®·µ»Ø¸øµ÷ÓÃÕß
317 2. ¶ÔÓÚZMGL,CPBR, COPS=?µÈÒ»ÌõÇëÇó»áÓжàÌõÖмä½á¹ûÇé¿ö£¬»¹ÐèʹÓÃregister_inform_func×¢²áÖмä½á¹ûµÄ´¦Àíº¯Êý
318 * @warning
319 */
320static int send_req_and_wait(char *req_at,int len,char *info_fmt,void **pval, int safe_parm_check,int timeout,int direct)
321{
322 int my_handle = 0;
323 char *prefix = NULL;
324 MSG_BUF rsp_msg = {0};
325 long msgSize = sizeof(MSG_BUF)-sizeof(long);
326 int iRet = 0;
327 int module_id = MODULE_ID_APP_DYNAMIC_BASE;//ÄÚ²¿¶¯Ì¬»ñȡԴģ¿éID£¬³õʼֵΪMODULE_ID_ATDYNAMIC_BASE
328 int err = 0;
329 int at_send_cnt = 0;
330
331 if(len > DATA_BUFFER_MAX-12)
332 at_assert(0);
333 at_assert(((info_fmt==NULL&&pval==NULL)||(info_fmt!=NULL&&pval!=NULL)));
334 //¶¯Ì¬´´½¨ÁÙʱµÄÏûÏ¢¶ÓÁйܵÀ
335 //msggetʹÓòÎÊýIPC_CREAT | IPC_EXCL| 0600£¬Åжϵ±Ç°module_idµÄÏûÏ¢¶ÓÁÐÊÇ·ñ
336 //´æÔÚ£¬Èç¹û²»´æÔÚ£¬Ö±½Ó´´½¨ÐµÄÏûÏ¢¶ÓÁУ»Èç¹û´æÔÚ£¬·µ»ØÖµÎ´-1£¬È»ºómodule_id
337 //Öµ¼Ó1£¬²»¶ÏÑ­»·Ö±µ½ÕÒµ½Ã»ÓÐʹÓõÄmodule_idÖµ
338 while((my_handle = msgget(module_id,IPC_CREAT | IPC_EXCL| 0600)) == -1)
339 {
340 module_id++;
341 if (module_id > MODULE_ID_APP_DYNAMIC_END)
342 //µ±module_id´óÓÚMODULE_ID_ATDYNAMIC_ENDֵʱ£¬Ö÷¶¯¶ÏÑÔ
343 at_assert(0);
344 }
345 //¸ù¾ÝÇëÇóATÃüÁîºÍ²éѯÓï·¨£¬ÕÒµ½¶ÔÓ¦µÄ²éѯǰ׺£¬¶¯Ì¬¸³Öµ¸ø³ö²Î£¬Íⲿ½øÐÐÄÚ´æµÄÊÍ·Å
346 get_at_req_prefix(req_at,&prefix);
347RESEND:
348 //¸øat_ctl·¢ËÍÏûÏ¢£¬·¢Ë͵ÄÏûÏ¢ÄÚÈÝÊÇatÃüÁî×Ö·û´®
349 at_send_cnt++;
350 at_assert(at_send_cnt<=RESEND_TIMES_MAX);
351 //ÓÉatctlÖ÷¿ØÄÚ²¿ÉèÖó¬Ê±µÈ´ý£¬ÖØ·¢ºó»áÓÉÖØ¹¹ÖØÉè¸Ã¶¨Ê±Æ÷
352 send_app_req(module_id,req_at,len,timeout,direct);
353 printf("prefix:%s\n",prefix);
354 while(1)
355 {
356 iRet = 0;
357 memset(&rsp_msg, 0x00, sizeof(MSG_BUF));
358 iRet = msgrcv(my_handle, &rsp_msg, msgSize, 0, 0);
359 if (iRet < 0)
360 {
361 continue;
362 }
363 //ËÀµÈÓ¦´ðÏûÏ¢
364 if(rsp_msg.usMsgCmd == MSG_CMD_SEND_AT_MSG_RSP)//Æ¥Åäµ½ÊÇMSG_CMD_SEND_AT_MSG_RSPʱ²Å´¦Àí
365 {
366 char *buf = rsp_msg.aucDataBuf;
367 char *at_paras = NULL;
368 char *rsp_head = NULL;
369 char *rsp_tail = NULL;
370 rsp_head = buf;
371 printf("recv MSG_CMD_SEND_AT_MSG_RSP, buf:%s\n",buf);
372 //¶ÔÓÚ²éѯÇëÇ󣬲¢ÌṩÁ˲ÎÊýÄÚ´æ¿Õ¼ä£¬Ôò½øÐвÎÊýµÄ½âÎö
373 if(prefix!=NULL && info_fmt!=NULL && pval!=NULL && ((rsp_head=strcasestr(buf,prefix))!=NULL))
374 {
375 //Ìø¹ýÖмä½á¹ûµÄÍ·²¿£¬ÕÒµ½²ÎÊýµÄÊ×µØÖ·
376 rsp_head = rsp_head+strlen(prefix)+2;
377 if((rsp_tail = strchr(rsp_head,'\0')) == NULL)//µ±Éϱ¨µÄÖмä½á¹ûÖÐûÓÐ\r\nʱ£¬ËµÃ÷½á¹ûÓÐÎÊÌ⣬ֱ½Ó¶ÏÑÔ
378 at_assert(0);
379
380 if(strcmp(info_fmt,"%s") == 0)
381 //Èç¹û²»ÏëÓÉÄÚ²¿½øÐвÎÊýµÄ¸ñʽ»¯½âÎöʱ£¬Ö±½Ó½«È«²¿²ÎÊýÄÚÈÝÈ¡³öÀ´¸³Öµµ½pval[0]ÖУ¬²»×öÈκνâÎö
382 strncpy((char *)*pval, rsp_head, (int)(rsp_tail-rsp_head));
383 else//ÐèÒª²ÎÊý½âÎöʱ£¬½«Öмä½á¹û´ÓÉϱ¨½á¹ûÖгé³öÀ´£¨Éϱ¨µÄ½á¹ûÖпÉÄÜÓÐOK´æÔÚ£©
384 {
385 char *at_paras = malloc(strlen(rsp_head)+1);
386 if(at_paras == NULL) at_assert(0);
387 memset(at_paras, 0, strlen(rsp_head)+1);
388 memcpy(at_paras, rsp_head, (int)(rsp_tail-rsp_head));
389
390 if(safe_parm_check)
391 err = parse_param_safe(info_fmt, at_paras, pval);//°´ÕÕ¸ñʽ½âÎö²ÎÊý
392 else
393 err = parse_param(info_fmt, at_paras, pval);//°´ÕÕ¸ñʽ½âÎö²ÎÊý
394 printf("rsp parse_param_safe return ERR=%d!!!!!!\n",err);
395 printf("at_paras=%s!!!!!!\n",at_paras);
396
397 if(at_paras != NULL)
398 free(at_paras);
399 //²ÎÊý²»ºÏ·¨£¬¿ÉÄÜÊÇ´®¿ÚͨÐÅÒì³£Ôì³ÉµÄ
400 else if(err != AT_PARSE_OK)
401 goto RESEND;
402 }
403 }
404 //±ê×¼3GPPµÄ´íÎ󣬲»½øÐÐÖØ´«³¢ÊÔ
405 if((rsp_head = strstr(buf,"ERROR")) != NULL)
406 {
407 //Èç¹ûÕÒ²»µ½Ã°ºÅ£¬ËµÃ÷ERRORûÓдíÎóÂë
408 if(strchr(rsp_head,':') != NULL)
409 sscanf(rsp_head+strlen("ERROR")+2, "%d", &err);
410 printf("server return ERR=%d, at_send_cnt=%d!!!!!!!\n",err,at_send_cnt);
411 //ÖÐÐË΢оƬ״̬»úÒì³££¬Ò»°ãΪ˽×ÔÖØÆô£¬ÐèÒª½øÐÐÕû»úµÄÖØÆô²Ù×÷
412 if(err == ATERR_ZXIC_ERR)
413 at_assert(!"ZXIC err!");
414
415 //¶ÔÓÚÏòÖÐÐË΢оƬ·¢ËÍÇëÇóʱ£¬»Ø¸´´íÎóÂëΪATERR_DROP_MARK¡¢ATERR_NO_PRINT¡¢ATERR_STR_TOO_LONG¡¢ATWARN_DROP_RN¡¢
416 //ATWARN_LACK_PARAM¡¢ATERR_WAIT_REQ_END_TIMEOUT¡¢ATERR_WAIT_RSP_TIMEOUTºÍATERR_UNKNOWNING_CMDʱ¶¼±ØÐëÖØ·¢
417 else if((err>=ATERR_DROP_MARK && err<=ATWARN_LACK_PARAM) || err == ATERR_WAIT_REQ_END_TIMEOUT || err == ATERR_WAIT_RSP_TIMEOUT
418 || err == ATERR_UNKNOWN_CMD)
419 goto RESEND;
420 break;
421 }
422 //¶ÔÓÚÉèÖÃÀàÃüÁîºÍһЩִÐÐÀàÃüÁֻÓÐOKÉϱ¨Ã»ÓÐÖмä½á¹ûÉϱ¨
423 else if((rsp_head = strstr(buf,"OK")) != NULL)
424 {
425 //ÖÐÐË΢»Ø¸´µÄ½á¹ûÈç¹û½âÎö³öµÄ²ÎÊý²»ºÏ·¨£¬Á¢¼´ÖØÐ·¢ËÍÇëÇó
426 if(err>=ATERR_DROP_MARK && err<=ATWARN_LACK_PARAM)
427 goto RESEND;
428 break;
429 }
430 }
431 }
432 if(prefix!=NULL)
433 free(prefix);
434 //²éѯÃüÁî½á¹û·µ»Øºó£¬Ïú»Ù֮ǰ´´½¨µÄÏûÏ¢¶ÓÁÐ
435 if(msgctl(my_handle,IPC_RMID,0) < 0)
436 printf("%d:%s",errno,strerror(errno));
437 return err;
438}
439
440//ÄÚ²¿¶ÔÓÚÎïÀíͨµÀÒì³£Ôì³ÉµÄÊý¾Ý´íÎó½øÐÐÖØ·¢³¢ÊÔµÄÈÝ´í£¬timeoutΪºÁÃëΪµ¥Î»µÄµÈ´ý½á¹ûʱ³¤
441int get_modem_info(char *req_at,char *info_fmt,void **pval, int timeout)
442{
443 return send_req_and_wait(req_at,strlen(req_at),info_fmt,pval,0,timeout,NEAR_PS);
444}
445
446//·¢ËÍÏûÏ¢½Ó¿Ú£¬³É¹¦·µ»Ø0£¬Ê§°Ü·µ»Ø·Ç0
447int at_send_msg(int source_id, int target_id, unsigned short Msg_cmd, unsigned short us_DataLen, unsigned char *pData, int msgflag)
448{
449 MSG_BUF stMsg;
450 int lRet = 0;
451 int lTgtMsgID = 0;
452 long msgSize = sizeof(MSG_BUF) - sizeof(long);
453
454 long errNo = 0;
455
456 memset(&stMsg, 0, sizeof(MSG_BUF));
457
458 lTgtMsgID = msgget(target_id, 0);
459 if (-1 == lTgtMsgID) {
460 return -1;
461 }
462
463 if ((us_DataLen > 0) && (NULL == pData)) {
464 return -1;
465 }
466
467 stMsg.usSourceModuleID = source_id;
468 stMsg.usTargetModuleID = target_id;
469 stMsg.usMsgCmd = Msg_cmd;
470 stMsg.usDataLen = us_DataLen;
471
472 if (us_DataLen > 0) {
473 memcpy(stMsg.aucDataBuf, pData, us_DataLen);
474 }
475
476AGAIN:
477
478 lRet = msgsnd(lTgtMsgID, &stMsg, msgSize, msgflag!=0?04000:0);//IPC_NOWAIT 04000
479 if (lRet < 0) {
480 if (errno == EINTR) {
481 goto AGAIN;
482 }
483 if (msgflag == 0) {
484 assert(0);
485 }
486 return -1;
487 }
488 return 0;
489}
490
491
492
493
494
495
496
497