Cellular.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450
  1. #include <time.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <unistd.h>
  6. #include <fcntl.h>
  7. #include <termios.h>
  8. #include <errno.h>
  9. #include "cJSON.h"
  10. #include "json_handle.h"
  11. #include "websocket_handle.h"
  12. #include "common.h"
  13. #include "sys.h"
  14. #include "Cellular.h"
  15. #include <pthread.h>
  16. #include "thread.h"
  17. #include "paras.h"
  18. #include "lock.h"
  19. #include "elog.h"
  20. #include "cfg.h"
  21. #if 0
  22. #define LOGE log_e
  23. #define LOGW log_w
  24. #define LOGD log_d
  25. #else
  26. #define LOGD printf
  27. #define LOGW printf
  28. #define LOGE printf
  29. #endif
  30. #define RETRY_TIMES 1
  31. #define BUF_LEN 2000
  32. #define AT_PORT "/dev/ttyUSB1"
  33. #define BAUD_RATE B115200
  34. #define QUERY_INTERVAL 1000 // 查询间隔,毫秒
  35. #define RSSI_DBM_MIN1 -113 // LTE、HSDPA网络最小信号值,对应RSSI值范围0-57
  36. #define RSSI_DBM_MIN2 -116 // TD-SCDMA网络最小信号值,对应RSSI值范围100-199
  37. #define CONTEXT_ID "1" //1~15
  38. #define CONNECT_ID "0" //0~11
  39. #define CONNECT_TIMEOUT "5"
  40. #define CHUNCK_SIZE 1460
  41. #define MQTT_VER 4 //3: v3.1 4: v3.1.1
  42. #define MQTT_WILL 0
  43. #define MQTT_RETAIN 0
  44. #define MQTT_MAX 6
  45. #define MQTT_QOS 2
  46. #define MQTT_CHUNCK_SIZE 1500
  47. enum {
  48. AT_OK=0,
  49. AT_ERR,
  50. AT_TOUT,
  51. };
  52. enum {
  53. AT_STAT_UNINIT=0,
  54. AT_STAT_INITED,
  55. };
  56. enum {
  57. SOCK_TCP=0,
  58. SOCK_UDP,
  59. };
  60. enum {
  61. URC_SOCK=0,
  62. URC_MQTT,
  63. URC_GNSS,
  64. URC_NTP,
  65. URC_SMS,
  66. URC_CALL,
  67. URC_ERR,
  68. };
  69. typedef int (*urc_fn_t)(int urc, void *data, int len);
  70. typedef struct {
  71. int cmd;
  72. char para[100];
  73. }at_para_t;
  74. typedef struct {
  75. char *cmd;
  76. char *ok;
  77. char *err;
  78. int timeout; //ms
  79. }at_cmd_t;
  80. typedef struct {
  81. int cmd;
  82. char *buf;
  83. int dlen;
  84. int blen;
  85. }at_buf_t;
  86. typedef struct {
  87. at_buf_t rx;
  88. at_buf_t tx;
  89. }at_data_t;
  90. typedef struct {
  91. void *h;
  92. int sock;
  93. int cont_id; //1~15
  94. int cont_type; //1:ipv4 2:ipv6 3:ipv4+ipv6
  95. int conn_id;
  96. char apn[64];
  97. char username[128];
  98. char password[128];
  99. int authentication; //0:none 1:PAP 2:CHAP 3:PAP OR CHAP
  100. int cdma_pwd; //0:no save 1:save
  101. }sock_conn_t;
  102. typedef struct {
  103. char host[100];
  104. uint16_t port;
  105. char username[64];
  106. char password[64];
  107. }mqtt_para_t;
  108. typedef struct {
  109. int flag;
  110. int qos;
  111. int retain;
  112. char topic[256];
  113. char message[256];
  114. int message_len;
  115. }mqtt_will_t;
  116. typedef struct {
  117. char product_key[64]; //aliyun used
  118. char device_name[64]; //aliyun used
  119. char device_secret[64]; //aliyun/ used
  120. char product_id[64]; //onenet used
  121. char access_key[64]; //onenet used
  122. int hw_time_enable; //huawei used
  123. char product_secret[64]; //huawei used
  124. char node_id[64];
  125. }mqtt_plat_t;
  126. typedef struct {
  127. void *h;
  128. int ver; //3:v3.1 4:v3.1.1
  129. int pdpId; //1~15
  130. int cliId;
  131. uint16_t msgId;
  132. int qos;
  133. int retain;
  134. mqtt_will_t will;
  135. int timeout; //1~60, default:5
  136. int retry_times;
  137. int timeout_notice;
  138. int clean_session;
  139. int keep_alive_time; //0~3600 seconds default:120, 0:do not disconnect
  140. int ssl_enable;
  141. int ssl_ctx_id;
  142. int msg_recv_mode; //0: urc, 1: not urc
  143. int msg_len_enable;
  144. int ping_interval; //5~60, default:5
  145. int proto_chk_enable; //0:no check 1:check
  146. int send_mode; //0:string 1:hex
  147. int recv_mode; //0:string 1:hex
  148. int view_mode; //0:no echo 1:echo
  149. int edit_mode; //0:timeout not quit 1:timeout quit
  150. int edit_time; //1~120 seconds
  151. mqtt_plat_t plat;
  152. }mqtt_conn_t;
  153. typedef struct {
  154. int fd;
  155. CellInfo_t *pcell;
  156. lock_t lck;
  157. int prod_id;
  158. int stat;
  159. cell_all_t all;
  160. int curcmd; //current cmd
  161. mqtt_conn_t mq_conn[MQTT_MAX];
  162. at_data_t data[AT_CMD_MAX];
  163. GlobalDeviceManager *dm;
  164. }cell_handle_t;
  165. static cell_handle_t ceHandle={0};
  166. static at_cmd_t all_cmd[AT_CMD_MAX]={
  167. //genernal cmd
  168. {"AT", "OK", "ERROR", 300}, //测试命令
  169. {"ATI", "OK", "ERROR", 300}, //显示MT的ID信息
  170. {"AT+GMI", "OK", "ERROR", 300}, //请求制造商信息
  171. {"AT+GMM", "OK", "ERROR", 300}, //请求TA型号ID
  172. {"AT+GMR", "OK", "ERROR", 300}, //请求TA固件版本ID
  173. {"AT+CGMI", "OK", "ERROR", 300}, //同GMI
  174. {"AT+CGMM", "OK", "ERROR", 300}, //同GMM
  175. {"AT+CGMR", "OK", "ERROR", 300}, //同GMR
  176. {"AT+GSN", "OK", "ERROR", 300}, //请求国际移动设备识别码(IMEI)
  177. {"AT+CGSN", "OK", "ERROR", 300}, //同GSN
  178. {"AT&F", "OK", "ERROR", 300}, //重置AT命令设置为出厂设置
  179. {"AT&V", "OK", "ERROR", 300}, //显示当前配置
  180. {"AT&W", "OK", "ERROR", 300}, //存储当前设置到用户自定义配置文件
  181. {"ATZ", "OK", "ERROR", 300}, //从用户定义配置文件还原所有AT命令设置
  182. {"ATQ", "OK", "ERROR", 300}, //设置结果码回显模式
  183. {"ATV", "OK", "ERROR", 300}, //TA响应格式
  184. {"ATE0", "OK", "ERROR", 300}, //设置命令回显模式
  185. {"ATS3", "OK", "ERROR", 300}, //设置命令行终止符
  186. {"ATS4", "OK", "ERROR", 300}, //设置响应格式字符
  187. {"ATS5", "OK", "ERROR", 300}, //设置命令行编辑字符
  188. {"ATX", "OK", "ERROR", 300}, //设置CONNECT结果码格式和检测呼叫进程
  189. {"AT+CFUN", "OK", "ERROR", 15000}, //设置功能模式
  190. {"AT+CMEE", "OK", "ERROR", 300}, //设置错误讯息格式
  191. {"AT+CSCS", "OK", "ERROR", 300}, //选择TE字符集
  192. {"AT+QURCCFG", "OK", "ERROR", 300}, //配置URC指示选项
  193. {"AT+QPPPDROP", "OK", "ERROR", 300}, //终止PPP连接
  194. //serial cmd
  195. {"AT&C", "OK", "ERROR", 300}, //设置DCD信号模式
  196. {"AT&D", "OK", "ERROR", 300}, //设置DTR信号模式
  197. {"AT+IFC", "OK", "ERROR", 300}, //设置串口的流控方式
  198. {"AT+IPR", "OK", "ERROR", 300}, //设置固定的串口通信波特率
  199. //status cmd
  200. {"AT+CPAS", "OK", "ERROR", 300}, //查询ME活动状态
  201. {"AT+CEER", "OK", "ERROR", 300}, //上报扩展错误
  202. {"AT+QCFG", "OK", "ERROR", 300}, //扩展命令, 较多子命令
  203. {"AT+QINDCFG", "OK", "ERROR", 300}, //控制URC上报
  204. //sim cmd
  205. {"AT+CIMI", "OK", "ERROR", 300}, //查询IMSI
  206. {"AT+CLCK", "OK", "ERROR", 5000}, //功能锁定
  207. {"AT+CPIN", "OK", "ERROR", 5000}, //PIN管理
  208. {"AT+CPWD", "OK", "ERROR", 5000}, //修改密码
  209. {"AT+CSIM", "OK", "ERROR", 300}, //通用(U)SIM卡访问
  210. {"AT+CRSM", "OK", "ERROR", 300}, //(U)SIM卡受限访问
  211. {"AT+QCCID", "OK", "ERROR", 300}, //查询ICCID
  212. {"AT+QPINC", "OK", "ERROR", 300}, //获取PIN剩余重试次数
  213. {"AT+QINISTAT", "OK", "ERROR", 300}, //查询(U)SIM卡初始化状态
  214. {"AT+QSIMDET", "OK", "ERROR", 300}, //(U)SIM卡检测
  215. {"AT+QSIMSTAT", "OK", "ERROR", 300}, //(U)SIM卡插拔状态上报
  216. {"AT+QDSIM", "OK", "ERROR", 50000}, //选择(U)SIM卡
  217. //network service cmd
  218. {"AT+COPS", "OK", "ERROR", 300000}, //选择运营商
  219. {"AT+CREG", "OK", "ERROR", 300}, //CS域网络注册状态
  220. {"AT+CSQ", "OK", "ERROR", 300}, //查询信号强度
  221. {"AT+CPOL", "OK", "ERROR", 300}, //配置首选运营商列表
  222. {"AT+COPN", "OK", "ERROR", 300}, //查询运营商名称列表
  223. {"AT+CTZU", "OK", "ERROR", 300}, //自动更新时区
  224. {"AT+CTZR", "OK", "ERROR", 300}, //上报时区变化
  225. {"AT+QLTS", "OK", "ERROR", 500}, //获取通过网络同步的最新时间
  226. {"AT+QNWINFO", "OK", "ERROR", 300}, //查询网络信息
  227. //call cmd
  228. {"ATA", "OK", "ERROR", 90000}, //呼叫应答
  229. {"ATD", "OK", "ERROR", 5000}, //发起呼叫
  230. {"AT+COLP", "OK", "ERROR", 15000}, //被叫线路识别显示
  231. {"AT+ATH", "OK", "ERROR", 90000}, //挂断现有连接
  232. {"AT+CHUP", "OK", "ERROR", 90000}, //挂断语音通话
  233. {"+++", "OK", "ERROR", 300}, //从数据模式切换至命令模式
  234. {"AT+ATO", "OK", "ERROR", 300}, //从命令模式切换至数据模式
  235. {"AT+ATSO", "OK", "ERROR", 300}, //设置自动应答前振铃次数
  236. {"AT+CLCC", "OK", "ERROR", 300}, //查询当前 ME 呼叫
  237. {"AT^DSCI", "OK", "ERROR", 300}, //指示呼叫状态
  238. //phonebook cmd
  239. {"AT+CNUM", "OK", "ERROR", 300}, //查询本机号码
  240. {"AT+CPBF", "OK", "ERROR", 300}, //搜索电话本
  241. {"AT+CPBR", "OK", "ERROR", 300}, //读取电话本条目
  242. {"AT+CPBS", "OK", "ERROR", 300}, //设置电话本存储器
  243. {"AT+CPBW", "OK", "ERROR", 300}, //写入电话本
  244. //sms cmd
  245. {"AT+CSMS", "OK", "ERROR", 300}, //选择短消息服务类型
  246. {"AT+CMGF", "OK", "ERROR", 300}, //配置短消息模式
  247. {"AT+CSCA", "OK", "ERROR", 300}, //设置短消息服务中心地址
  248. {"AT+CPMS", "OK", "ERROR", 300}, //配置短消息首选存储位置
  249. {"AT+CMGD", "OK", "ERROR", 300}, //删除短消息
  250. {"AT+CMGL", "OK", "ERROR", 300}, //按状态读取短消息
  251. {"AT+CMGR", "OK", "ERROR", 300}, //按照索引读取短消息
  252. {"AT+CMGS", ">", "ERROR", 120000}, //发送短消息
  253. {"AT+CMMS", "OK", "ERROR", 300}, //发送多条短消息
  254. {"AT+CMGW", "OK", "ERROR", 300}, //存储短消息
  255. {"AT+CMSS", "OK", "ERROR", 300}, //从存储器发送短消息
  256. {"AT+CNMA", "OK", "ERROR", 300}, //新短消息确认
  257. {"AT+CNMI", "OK", "ERROR", 300}, //设置短消息上报方式
  258. {"AT+CSCB", "OK", "ERROR", 300}, //选择小区广播消息类型
  259. {"AT+CSDH", "OK", "ERROR", 300}, //设置文本模式下参数显示
  260. {"AT+CSMP", "OK", "ERROR", 300}, //设置短消息文本模式参数
  261. {"AT+QCMGS", "OK", "ERROR", 120000}, //发送级联短消息
  262. {"AT+QCMGR", "OK", "ERROR", 300}, //读取级联短消息
  263. //gps cmd
  264. {"AT+QGPSCFG", "OK", "ERROR", 300}, //配置GNSS
  265. {"AT+QGPSDEL", "OK", "ERROR", 300}, //删除辅助数据
  266. {"AT+QGPS", "OK", "ERROR", 300}, //打开GNSS
  267. {"AT+QGPSEND", "OK", "ERROR", 300}, //关闭GNSS
  268. {"AT+QGPSLOC", "OK", "ERROR", 300}, //获取定位信息
  269. {"AT+QGPSNMEA", "OK", "ERROR", 300}, //获取指定的NMEA语句
  270. {"AT+QAGPS", "OK", "ERROR", 300}, //启用/禁用AGPS
  271. //tcp/udp cmd
  272. {"AT+QICFG", "OK", "ERROR", 300}, //配置可选参数
  273. {"AT+QICSGP", "OK", "ERROR", 300}, //配置TCP/IP场景参数
  274. {"AT+QIACT", "OK", "ERROR", 10000}, //激活PDP场景
  275. {"AT+QIDEACT", "OK", "ERROR", 10000}, //去激活PDP场景
  276. {"AT+QIOPEN", "OK", "ERROR", 300}, //打开Socket服务
  277. {"AT+QICLOSE", "OK", "ERROR", 300}, //关闭Socket服务
  278. {"AT+QISTATE", "OK", "ERROR", 300}, //查询Socket服务状态
  279. {"AT+QISEND", ">", "ERROR", 300}, //发送数据
  280. {"AT+QIRD", "OK", "ERROR", 300}, //读取收到的TCP/IP数据
  281. {"AT+QISENDEX", "OK", "ERROR", 300}, //发送16进制字符串数据
  282. {"AT+QISWTMD", "OK", "ERROR", 300}, //切换数据访问模式
  283. {"AT+QNTP", "OK", "ERROR", 300}, //进行远程服务器Ping操作
  284. {"AT+QPING", "+QPING", "ERROR", 3000}, //使用NTP服务器同步本地时间
  285. {"AT+QIDNSCFG", "OK", "ERROR", 300}, //配置DNS服务器地址
  286. {"AT+QIDNSGIP", "OK", "ERROR", 300}, //用域名获取IP地址
  287. {"AT+QISDE", "OK", "ERROR", 300}, //控制是否回显AT+QISEND要发送的
  288. {"AT+QIGETERROR", "OK", "ERROR", 300}, //查询上一个AT命令错误代码
  289. //mqtt cmd
  290. {"AT+QMTCFG", "OK", "ERROR", 300}, //配置MQTT可选参数
  291. {"AT+QMTOPEN", "OK", "ERROR", 300}, //打开MQTT客户端网络
  292. {"AT+QMTCLOSE", "OK", "ERROR", 300}, //关闭MQTT客户端网络
  293. {"AT+QMTCONN", "OK", "ERROR", 300}, //连接客户端到MQTT服务器
  294. {"AT+QMTDISC", "OK", "ERROR", 300}, //客户端断开与MQTT服务器的连接
  295. {"AT+QMTSUB", "OK", "ERROR", 300}, //订阅主题
  296. {"AT+QMTUNS", "OK", "ERROR", 300}, //退订主题
  297. {"AT+QMTPUBEX", ">", "ERROR", 300}, //发布消息
  298. {"AT+QMTRECV", "OK", "ERROR", 300}, //从缓存中读取消息
  299. //domain cmd
  300. {"AT+CGATT", "OK", "ERROR", 140000}, //PS域附着或去附着
  301. {"AT+CGDCONT", "OK", "ERROR", 3000}, //定义PDP上下文
  302. {"AT+CGQREQ", "OK", "ERROR", 300}, //服务质量参数请求
  303. {"AT+CGQMIN", "OK", "ERROR", 300}, //服务质量参数(最低可接受的)
  304. {"AT+CGEQREQ", "OK", "ERROR", 300}, //3G服务质量参数(请求的)
  305. {"AT+CGEQMIN", "OK", "ERROR", 300}, //3G 服务质量参数(最低可接受的)
  306. {"AT+CGACT", "OK", "ERROR", 150000}, //PDP上下文激活/去激活
  307. {"AT+CGDATA", "OK", "ERROR", 300}, //进入数据传输状态
  308. {"AT+CGPADDR", "OK", "ERROR", 300}, //显示PDP地址
  309. {"AT+CGCLASS", "OK", "ERROR", 300}, //GPRS移动站点等级
  310. {"AT+CGEREG", "OK", "ERROR", 300}, //PS域网络注册状态
  311. {"AT+CGEREP", "OK", "ERROR", 300}, //分组域事件上报
  312. {"AT+CGSMS", "OK", "ERROR", 300}, //选择MO短消息服务
  313. {"AT+CEREG", "OK", "ERROR", 300}, //EPS网络注册状态
  314. {"AT+QGDCNT", "OK", "ERROR", 300}, //流量统计
  315. {"AT+QAUGDCNT", "OK", "ERROR", 300}, //自动保存流量统计
  316. {"AT+QNETDEVCTL", "OK", "ERROR", 2000}, //连接USB网卡
  317. {"AT+QCEERCATCFG", "OK", "ERROR", 300}, //配置2G/4G下AT+CEER返回格式
  318. //hardware cmd
  319. {"AT+QPOWD", "OK", "ERROR", 300}, //关闭模块
  320. {"AT+CCLK", "OK", "ERROR", 300}, //实时时钟
  321. {"AT+QSCLK", "OK", "ERROR", 300}, //禁用/启用睡眠模式
  322. };
  323. at_para_t init_para[]={
  324. {AT_CMD_AT, ""},
  325. {AT_CMD_ATE, "0"},
  326. {AT_CMD_CPIN, "?"},
  327. {AT_CMD_CREG, "?"},
  328. //{AT_CMD_CGDCONT, ""},
  329. {AT_CMD_QICSGP, ""},
  330. {AT_CMD_QIDEACT, "="CONTEXT_ID},
  331. {AT_CMD_QIACT, "="CONTEXT_ID},
  332. {AT_CMD_QCFG, "=\"usbnet\",1"}, //set module to ecm mode
  333. {AT_CMD_QNETDEVCTL, "=3,"CONTEXT_ID",0"},
  334. {AT_CMD_CMGF, "=1"},
  335. {AT_CMD_CSCS, "=GSM"},
  336. #ifdef GNSS_ENABLE
  337. {AT_CMD_QGPS, "=1"},
  338. {AT_CMD_QGPSCFG, "=\"gnssconfig\",5"},
  339. {AT_CMD_QGPSCFG, "=\"gpsnmeatype\",1"},
  340. #endif
  341. {-1, ""},
  342. };
  343. ///////////////////////////////////////////////////////////////////////////////////////////////////////
  344. static at_cmd_t* data_of(int cmd)
  345. {
  346. return &all_cmd[cmd];
  347. }
  348. static int rssi_to_dbm(int rssi)
  349. {
  350. int dbm=0;
  351. if(rssi>=0 && rssi<57) {
  352. dbm = RSSI_DBM_MIN1+rssi*2;
  353. }
  354. else if(rssi>=100 && rssi<199) {
  355. dbm = RSSI_DBM_MIN2+rssi-100;
  356. }
  357. return dbm;
  358. }
  359. static char* str_get(char *buf, char c)
  360. {
  361. int i,len=strlen(buf);
  362. for(i=0; i<len; i++) {
  363. if(buf[i]==c) return buf+i;
  364. }
  365. return buf;
  366. }
  367. static int mem_find(char *buf, int len, char *str)
  368. {
  369. int i,slen=strlen(str);
  370. for(i=0; i<=len-slen; i++) {
  371. if(memcmp(buf+i, str, slen)==0) {
  372. return 0;
  373. }
  374. }
  375. return -1;
  376. }
  377. static void mem_print(char *buf, int len)
  378. {
  379. int i;
  380. for(i=0; i<len; i++) {
  381. LOGD("%d, ", buf[i]);
  382. if((i+1)%16==0) LOGD("\n");
  383. }
  384. LOGD("\n");
  385. }
  386. static int nic_set(int nic, int on)
  387. {
  388. char tmp[200],path[100];
  389. const char *ifname=sys_get_ifname(nic);
  390. sprintf(path, "/var/run/udhcpc_%s.pid", ifname);
  391. sprintf(tmp, "kill $(cat %s)", path);
  392. system(tmp);
  393. sprintf(tmp, "ip link set dev %s %s", ifname, on?"up":"down");
  394. system(tmp);
  395. if(on) {
  396. sprintf(tmp, "udhcpc -R -b -i %s -p %s ", ifname, path);
  397. system(tmp);
  398. }
  399. return 0;
  400. }
  401. static int cell_reset(cell_handle_t *h);
  402. static int cell_sock_init(cell_handle_t *h);
  403. static int cell_get_info(cell_handle_t *h, cell_info_t *info);
  404. static int cell_get_stat(cell_handle_t *h, cell_stat_t *stat);
  405. static void conn_init(cell_handle_t *h, mqtt_conn_t *conn);
  406. //////////////////////////////////////////////////////////////////////////////////
  407. static int port_setup(cell_handle_t *h, int speed);
  408. static void msleep(int ms)
  409. {
  410. usleep(ms*1000);
  411. }
  412. static void reset_var( cell_handle_t *h)
  413. {
  414. h->stat=AT_STAT_UNINIT;
  415. h->all.stat.dbm = 0;
  416. h->all.stat.ber = 0;
  417. h->all.stat.nsta = 0;
  418. h->all.stat.altitude = 0;
  419. sprintf(h->all.stat.latitude, "-");
  420. sprintf(h->all.stat.longitude, "-");
  421. h->all.stat.utctime = 0;
  422. h->all.stat.linked = 0;
  423. sprintf(h->all.info.apn, "-");
  424. sprintf(h->all.info.cnum, "-");
  425. sprintf(h->all.info.imsi, "-");
  426. sprintf(h->all.info.imei, "-");
  427. sprintf(h->all.info.isp, "-");
  428. }
  429. static int port_init(cell_handle_t *h)
  430. {
  431. h->fd = open(AT_PORT, O_RDWR | O_NOCTTY | O_NONBLOCK);
  432. if(h->fd<0) {
  433. return -1;
  434. }
  435. port_setup(h, BAUD_RATE);
  436. return 0;
  437. }
  438. static int port_deinit(cell_handle_t *h)
  439. {
  440. if(h->fd<0) {
  441. return -1;
  442. }
  443. int r=close(h->fd);
  444. h->fd=-1;
  445. sleep(2);
  446. return r;
  447. }
  448. static int port_read(cell_handle_t *h, void *data, int len)
  449. {
  450. return read(h->fd, data, len);
  451. }
  452. static int port_write(cell_handle_t *h, void *data, int len)
  453. {
  454. return write(h->fd, data, len);
  455. }
  456. static int port_setup(cell_handle_t *h, int speed)
  457. {
  458. struct termios tty;
  459. if (tcgetattr(h->fd, &tty) != 0) {
  460. LOGE("Read 4G Module tcgetattr!!!\n");
  461. return -1;
  462. }
  463. cfsetospeed(&tty, speed);
  464. cfsetispeed(&tty, speed);
  465. tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8; // 8-bit chars
  466. tty.c_iflag &= ~IGNBRK; // disable break processing
  467. tty.c_lflag = 0; // no signaling chars, no echo,
  468. // no canonical processing
  469. tty.c_oflag = 0; // no remapping, no delays
  470. tty.c_cc[VMIN] = 0; // read doesn't block
  471. tty.c_cc[VTIME] = 5; // 0.5 seconds read timeout
  472. tty.c_iflag &= ~(IXON | IXOFF | IXANY); // shut off xon/xoff ctrl
  473. tty.c_cflag |= (CLOCAL | CREAD); // ignore modem controls,
  474. // enable reading
  475. tty.c_cflag &= ~(PARENB | PARODD); // shut off parity
  476. tty.c_cflag |= 0;
  477. tty.c_cflag &= ~CSTOPB;
  478. tty.c_cflag &= ~CRTSCTS;
  479. if (tcsetattr(h->fd, TCSANOW, &tty) != 0) {
  480. LOGE("Read 4G Module tcsetattr!!!\n");
  481. return -1;
  482. }
  483. return 0;
  484. }
  485. static int port_wait(cell_handle_t *h, int timeout)
  486. {
  487. int r;
  488. char buf[100];
  489. fd_set fset;
  490. struct timeval tv;
  491. tv.tv_sec = timeout/1000;
  492. tv.tv_usec = (timeout%1000)*1000;
  493. FD_ZERO(&fset); FD_SET(h->fd, &fset);
  494. r = select(h->fd + 1, &fset, NULL, NULL, &tv);
  495. if (r<=0) {
  496. if(r<0) {
  497. //LOGE("port_wait error %s\n", strerror(errno));
  498. }
  499. else {
  500. //LOGE("port_wait timeout\n");
  501. }
  502. }
  503. return r;
  504. }
  505. static int port_recv(cell_handle_t *h, char *buf, int buflen, int timeout)
  506. {
  507. int r;
  508. r = port_wait(h, timeout);
  509. if(r<0) {
  510. return -1;
  511. }
  512. r = port_read(h, buf, buflen);
  513. if(r<=0) {
  514. return -1;
  515. }
  516. return r;
  517. }
  518. static int send_cmd(cell_handle_t *h, int cmd, char *para, char *rcv, int rcvlen)
  519. {
  520. int i,r,l,tout,finish=0;
  521. int tlen=0,buflen,wait_step=10;
  522. char buf[BUF_LEN],*pbuf;
  523. at_cmd_t *pcmd=data_of(cmd);
  524. sprintf(buf, "%s%s\r", pcmd->cmd, para?para:"");
  525. LOGD("[tx]: %s\n", buf);
  526. for (i = 0; i < RETRY_TIMES; i++)
  527. {
  528. tout = 0;
  529. port_write(h, buf, strlen(buf));
  530. while (tout < pcmd->timeout)
  531. {
  532. l = port_wait(h, wait_step);
  533. if (l > 0)
  534. {
  535. if (rcv && rcvlen > 0)
  536. {
  537. pbuf = rcv;
  538. buflen = rcvlen;
  539. }
  540. else
  541. {
  542. pbuf = buf;
  543. buflen = sizeof(buf);
  544. }
  545. l = port_read(h, pbuf + tlen, buflen);
  546. if (l > 0)
  547. {
  548. tlen += l;
  549. pbuf[tlen] = 0;
  550. LOGD("[rx]: %s\n", pbuf);
  551. if (pcmd->ok && strlen(pcmd->ok) && strstr(pbuf, pcmd->ok))
  552. {
  553. return AT_OK;
  554. }
  555. if (pcmd->err && strlen(pcmd->err) && strstr(pbuf, pcmd->err))
  556. {
  557. // 指令不通则重置模块
  558. if (cmd != AT_CMD_CFUN && cmd != AT_CMD_QIDEACT )
  559. {
  560. cell_reset(h);
  561. }
  562. return AT_ERR;
  563. }
  564. }
  565. }
  566. tout += wait_step;
  567. usleep(1000);
  568. }
  569. }
  570. //AT不通重启串口
  571. if (cmd != AT_CMD_QIDEACT && cmd != AT_CMD_CFUN )
  572. {
  573. port_deinit(h);
  574. port_init(h);
  575. }
  576. return AT_TOUT;
  577. }
  578. static int send_data(cell_handle_t *h, void *data, int len, int timeout)
  579. {
  580. char buf[100];
  581. int l,tlen=0,tout=0,wait_step=10;
  582. l = port_write(h, data, len);
  583. if(l==len) {
  584. while(tout<timeout) {
  585. l = port_wait(h, wait_step);
  586. if(l>0) {
  587. l = port_read(h, buf, sizeof(buf));
  588. if(l>0) {
  589. tlen += l; buf[tlen] = 0;
  590. if(strstr(buf, "OK")) {
  591. return AT_OK;
  592. }
  593. else if(strstr(buf, "ERROR")) {
  594. return AT_ERR;
  595. }
  596. }
  597. }
  598. tout += wait_step;
  599. }
  600. }
  601. return AT_TOUT;
  602. }
  603. ////////////////////////////////////////////////////////////////////////////
  604. static int cell_reset(cell_handle_t *h)
  605. {
  606. reset_var(h);
  607. int r;
  608. r = send_cmd(h, AT_CMD_CFUN, "=1,1", NULL, 0);
  609. // if(r==AT_OK) {
  610. // h->stat = AT_STAT_UNINIT;
  611. //}
  612. sleep(5);//等待模块复位完成
  613. return (r==AT_OK)?0:-1;
  614. }
  615. static int cell_init(cell_handle_t *h)
  616. {
  617. int i,r;
  618. at_para_t *p=init_para;
  619. if(h->stat==AT_STAT_INITED) {
  620. return 0;
  621. }
  622. for(i=0;; i++) {
  623. if(p[i].cmd==-1) {
  624. break;
  625. }
  626. if(p[i].cmd==AT_CMD_CGDCONT) {
  627. sprintf(p[i].para, "=%s,\"IPV4V6\",\"%s\"", CONTEXT_ID, h->pcell->apn);
  628. }
  629. else if(p[i].cmd==AT_CMD_QICSGP) {
  630. sprintf(p[i].para, "=%s,3,\"%s\",\"%s\",\"%s\"", CONTEXT_ID, h->pcell->apn,h->pcell->username,h->pcell->password);
  631. }
  632. r = send_cmd(h, p[i].cmd, p[i].para, NULL, 0);
  633. if(r!=AT_OK) {
  634. break;
  635. }
  636. }
  637. if(r==0) {
  638. h->stat = AT_STAT_INITED;
  639. cell_get_info(h, &h->all.info);
  640. }
  641. return r?-1:0;
  642. }
  643. static int cell_deinit(cell_handle_t *h)
  644. {
  645. int r=-1;
  646. char buf[400];
  647. r = send_cmd(h, AT_CMD_QIDEACT, "="CONTEXT_ID, buf, sizeof(buf));
  648. if(r==AT_OK) {
  649. return r;
  650. }
  651. return r;
  652. }
  653. static int cell_get_info(cell_handle_t *h, cell_info_t *info)
  654. {
  655. int r;
  656. char buf[400];
  657. if(h->stat==AT_STAT_UNINIT) {
  658. return -1;
  659. }
  660. memset(info, 0, sizeof(cell_info_t));
  661. // r = send_cmd(h, AT_CMD_CNUM, NULL, buf, sizeof(buf));
  662. // if(r==AT_OK) {
  663. // r = sscanf(str_get(buf,'+'), "+CNUM: %*s,%s,%*d", info->cnum);
  664. // }
  665. r = send_cmd(h, AT_CMD_GSN, NULL, buf, sizeof(buf));
  666. if(r==AT_OK) {
  667. r = sscanf(str_get(buf,'+'), "%s", info->imei);
  668. }
  669. r = send_cmd(h, AT_CMD_CIMI, NULL, buf, sizeof(buf));
  670. if(r==AT_OK) {
  671. r = sscanf(str_get(buf,'+'), "%s", info->imsi);
  672. }
  673. r = send_cmd(h, AT_CMD_CGDCONT, "?", buf, sizeof(buf));
  674. if(r==AT_OK) {
  675. r = sscanf(str_get(buf,'+'), "+CGDCONT: %*d,\"%*[^\"]\",\"%[^\"]", info->apn);
  676. }
  677. r = send_cmd(h, AT_CMD_COPS, "?", buf, sizeof(buf));
  678. if(r==AT_OK) {
  679. r = sscanf(str_get(buf,'+'), "+COPS: %*d,%*d,\"%[^\"]", info->isp);
  680. }
  681. LOGD("___info->apn: %s\n", info->apn);
  682. LOGD("___info->isp: %s\n", info->isp);
  683. LOGD("___info->imei: %s\n", info->imei);
  684. LOGD("___info->imsi: %s\n", info->imsi);
  685. LOGD("___info->cnum: %s\n", info->cnum);
  686. return 0;
  687. }
  688. static int cell_get_sig(cell_handle_t *h, cell_stat_t *stat)
  689. {
  690. char buf[200];
  691. int rssi,ber,r=-1;
  692. if(h->stat==AT_STAT_UNINIT) {
  693. return -1;
  694. }
  695. r = send_cmd(h, AT_CMD_CSQ, NULL, buf, sizeof(buf));
  696. if (r != AT_OK)
  697. {
  698. return -1;
  699. }
  700. r = sscanf(str_get(buf, '+'), "%*s %d,%d", &rssi, &ber);
  701. if (r < 2)
  702. {
  703. return -1;
  704. }
  705. stat->dbm = rssi_to_dbm(rssi);
  706. stat->ber = ber;
  707. return 0;
  708. }
  709. static int cell_get_gnss(cell_handle_t *h, cell_stat_t *stat)
  710. {
  711. int r;
  712. char buf[200],utc[32],date[32];
  713. #ifdef GNSS_ENABLE
  714. if(h->stat==AT_STAT_UNINIT) {
  715. return -1;
  716. }
  717. r = send_cmd(h, AT_CMD_QGPSLOC, "=0", buf, sizeof(buf));
  718. if(r!=AT_OK) {
  719. return -1;
  720. }
  721. r = sscanf(str_get(buf,'+'), "+QGPSLOC: %s,%s,%s,%*d,%d,%*d,%*d,%*d,%*d,%s,%d", utc, stat->latitude, stat->longitude, &stat->altitude, date, &stat->nsta);
  722. if(r<6) {
  723. return -1;
  724. }
  725. //
  726. #endif
  727. return 0;
  728. }
  729. static int cell_get_ping(cell_handle_t *h, cell_stat_t *stat)
  730. {
  731. int r,ret;
  732. char buf[200],utc[32],date[32];
  733. if(h->stat==AT_STAT_UNINIT) {
  734. return -1;
  735. }
  736. r = send_cmd(h, AT_CMD_QPING, "=1,\"www.baidu.com\",3,2,", buf, sizeof(buf));
  737. if(r!=AT_OK) {
  738. stat->linked = 0;
  739. return -1;
  740. }
  741. r = sscanf(str_get(buf,'+'), "+QPING: %d", &ret);
  742. if(r<1) {
  743. stat->linked = 0;
  744. return -1;
  745. }
  746. LOGD("QPING: %d\n", ret);
  747. if(ret==0) {
  748. stat->linked = 1;
  749. }
  750. else {
  751. stat->linked = 0;
  752. }
  753. return 0;
  754. }
  755. static int cell_get_time(cell_handle_t *h, cell_stat_t *stat)
  756. {
  757. int r,offset;
  758. char opr,buf[200],date[24],time[24];
  759. struct tm utc_tm;
  760. if(h->stat==AT_STAT_UNINIT) {
  761. return -1;
  762. }
  763. r = send_cmd(h, AT_CMD_QLTS, "=1", buf, sizeof(buf));
  764. if(r!=AT_OK) {
  765. return -1;
  766. }
  767. r = sscanf(str_get(buf,'+'), "+QLTS: \"%d/%d/%d,%d:%d:%d",&utc_tm.tm_year,&utc_tm.tm_mon,&utc_tm.tm_mday,&utc_tm.tm_hour,&utc_tm.tm_min,&utc_tm.tm_sec);
  768. if(r<6) {
  769. return -1;
  770. }
  771. stat->utctime = mktime2(&utc_tm, 0);
  772. //print_time("999", &utc_tm);
  773. return 0;
  774. }
  775. static int cell_get_stat(cell_handle_t *h, cell_stat_t *stat)
  776. {
  777. if(h->stat==AT_STAT_UNINIT) {
  778. return -1;
  779. }
  780. cell_get_sig(h, stat);
  781. cell_get_gnss(h, stat);
  782. cell_get_ping(h, stat);
  783. cell_get_time(h, stat);
  784. return 0;
  785. }
  786. static int cell_sock_conn(cell_handle_t *h, int sock, char *host, uint16_t port, sock_conn_t *conn)
  787. {
  788. int r;
  789. char buf[200];
  790. if(h->stat==AT_STAT_UNINIT) {
  791. return -1;
  792. }
  793. sprintf(buf, "=%s,%s,%s,“%s”,%d,0,1", CONTEXT_ID, CONNECT_ID, (sock==SOCK_TCP)?"TCP":"UDP", host, port);
  794. r = send_cmd(h, AT_CMD_QIOPEN, buf, NULL, 0);
  795. if(r!=AT_OK) {
  796. return -1;
  797. }
  798. conn->h = h;
  799. conn->sock = sock;
  800. conn->cont_id = atoi(CONTEXT_ID);
  801. conn->conn_id = atoi(CONNECT_ID);
  802. return 0;
  803. }
  804. static int cell_sock_disconn(sock_conn_t *conn)
  805. {
  806. int r;
  807. char buf[200];
  808. cell_handle_t *h=(cell_handle_t*)conn->h;
  809. if(h->stat==AT_STAT_UNINIT) {
  810. return -1;
  811. }
  812. sprintf(buf, "=%s,%s", CONNECT_ID, CONNECT_TIMEOUT);
  813. r = send_cmd(h, AT_CMD_QICLOSE, buf, NULL, 0);
  814. if(r!=AT_OK) {
  815. return -1;
  816. }
  817. conn->h = NULL;
  818. conn->sock = -1;
  819. conn->cont_id = -1;
  820. conn->conn_id = -1;
  821. return 0;
  822. }
  823. static int cell_sock_send(sock_conn_t *conn, void *data, int len)
  824. {
  825. int r=0,rt,offset=0,remain_len,send_len;
  826. char buf[100],*pd=(char*)data;
  827. cell_handle_t *h=(cell_handle_t*)conn->h;
  828. if(h->stat==AT_STAT_UNINIT) {
  829. return -1;
  830. }
  831. sprintf(buf, "=%d,%d", conn->conn_id, len);
  832. rt = send_cmd(h, AT_CMD_QISEND, buf, NULL, 0);
  833. if(rt!=AT_OK) {
  834. return -1;
  835. }
  836. while(offset<len) {
  837. remain_len = len - offset;
  838. send_len = (remain_len>CHUNCK_SIZE)?CHUNCK_SIZE:remain_len;
  839. rt = send_data(h, data+offset, send_len, 500);
  840. if(rt!=AT_OK) {
  841. r = -1; break;
  842. }
  843. offset += send_len;
  844. }
  845. return r;
  846. }
  847. static int cell_sock_stat(sock_conn_t *conn)
  848. {
  849. int r;
  850. char buf[100];
  851. cell_handle_t *h=(cell_handle_t*)conn->h;
  852. if(h->stat==AT_STAT_UNINIT) {
  853. return -1;
  854. }
  855. r = send_cmd(h, AT_CMD_QISTATE, NULL, buf, sizeof(buf));
  856. if(r!=AT_OK) {
  857. return -1;
  858. }
  859. return 0;
  860. }
  861. static int cell_sock_connected(cell_handle_t *h)
  862. {
  863. return 0;
  864. }
  865. static mqtt_conn_t* get_mqtt_conn_free(cell_handle_t *h)
  866. {
  867. int i;
  868. for(i=0; i<MQTT_MAX; i++) {
  869. if(h->mq_conn[i].h==NULL) {
  870. return &h->mq_conn[i];
  871. }
  872. }
  873. return NULL;
  874. }
  875. static int cell_mqtt_init(cell_handle_t *h)
  876. {
  877. int i;
  878. at_cmd_t *pcmd=NULL;
  879. for(i=0; i<MQTT_MAX; i++) {
  880. h->mq_conn[i].cliId = i;
  881. }
  882. return 0;
  883. }
  884. static int cell_mqtt_conn(cell_handle_t *h, mqtt_para_t *para)
  885. {
  886. int r;
  887. char tmp[200],buf[100];
  888. mqtt_conn_t *conn=NULL;
  889. conn = get_mqtt_conn_free(h);
  890. if(!conn) {
  891. return -1;
  892. }
  893. conn_init(h, conn);
  894. sprintf(tmp, "=%d,\"%s\",%d", conn->cliId, para->host, para->port);
  895. r = send_cmd(h, AT_CMD_QMTOPEN, tmp, buf, sizeof(buf));
  896. if(r!=AT_OK) {
  897. return -1;
  898. }
  899. sprintf(tmp, "=%d,\"mqtt%d\",\"%s\",\"%s\"", conn->cliId, conn->cliId, para->username, para->password);
  900. r = send_cmd(h, AT_CMD_QMTCONN, tmp, buf, sizeof(buf));
  901. if(r!=AT_OK) {
  902. return -1;
  903. }
  904. conn->h = h;
  905. conn->qos = 2;
  906. conn->retain = 0;
  907. return 0;
  908. }
  909. static int cell_mqtt_disconn(mqtt_conn_t *conn)
  910. {
  911. int r;
  912. char tmp[100],buf[100];
  913. at_cmd_t *pcmd=NULL;
  914. sprintf(tmp, "=%d", conn->cliId);
  915. r = send_cmd(conn->h, AT_CMD_QMTDISC, tmp, buf, sizeof(buf));
  916. if(r!=AT_OK) {
  917. return -1;
  918. }
  919. r = send_cmd(conn->h, AT_CMD_QMTCLOSE, tmp, buf, sizeof(buf));
  920. if(r!=AT_OK) {
  921. return -1;
  922. }
  923. conn->msgId = 0;
  924. conn->h = NULL;
  925. return 0;
  926. }
  927. static int cell_mqtt_sub(mqtt_conn_t *conn, char *topic)
  928. {
  929. int r;
  930. char buf[500];
  931. at_cmd_t *pcmd=NULL;
  932. sprintf(buf, "=%d, %d, \"%s\",%d", conn->cliId, conn->msgId, topic, conn->qos);
  933. r = send_cmd(conn->h, AT_CMD_QMTSUB, buf, NULL, 0);
  934. if(r!=AT_OK) {
  935. return -1;
  936. }
  937. return 0;
  938. }
  939. static int cell_mqtt_pub(mqtt_conn_t *conn, char *topic, void *data, int len)
  940. {
  941. char buf[200],*pd=(char*)data;
  942. int r=0,rt,offset=0,remain_len,send_len;
  943. sprintf(buf, "=%d,%d,%d,%d,\"%s\",%d", conn->cliId, conn->msgId, conn->qos, conn->retain, topic, len);
  944. rt = send_cmd(conn->h, AT_CMD_QMTPUB, NULL, buf, sizeof(buf));
  945. if(rt!=AT_OK) {
  946. return -1;
  947. }
  948. while(offset<len) {
  949. remain_len = len - offset;
  950. send_len = (remain_len>CHUNCK_SIZE)?CHUNCK_SIZE:remain_len;
  951. rt = send_data(conn->h, data+offset, send_len, 500);
  952. if(rt!=AT_OK) {
  953. r = -1; break;
  954. }
  955. offset += send_len;
  956. }
  957. return 0;
  958. }
  959. static int cell_send_sms(cell_handle_t *h, char *number, char *txt)
  960. {
  961. int r=-1,rt;
  962. char tmp[100],buf[200];
  963. if(h->stat==AT_STAT_UNINIT) {
  964. return -1;
  965. }
  966. sprintf(tmp, "=\"%s\"", number);
  967. r = send_cmd(h, AT_CMD_CMGS, tmp, buf, sizeof(buf));
  968. if(r!=AT_OK) {
  969. return -1;
  970. }
  971. snprintf(buf, sizeof(buf), "%s\x1a", txt);
  972. r = send_data(h, buf, strlen(buf), 5000);
  973. return r;
  974. }
  975. static int cell_send_call(cell_handle_t *h, char *number, void *data, int len)
  976. {
  977. int r;
  978. char buf[100];
  979. if(h->stat==AT_STAT_UNINIT) {
  980. return -1;
  981. }
  982. sprintf(buf, "%s", number);
  983. r = send_cmd(h, AT_CMD_ATD, NULL, buf, sizeof(buf));
  984. if(r!=AT_OK) {
  985. return -1;
  986. }
  987. return 0;
  988. }
  989. /////////////////////////////////////////////////////////////////////////
  990. static int conn_cfg(mqtt_conn_t *conn, char *item, char *value)
  991. {
  992. int r;
  993. char tmp[100],buf[200];
  994. cell_handle_t *h=(cell_handle_t*)conn->h;
  995. if(h->stat==AT_STAT_UNINIT) {
  996. return -1;
  997. }
  998. sprintf(tmp, "=\"%s\",%s", item, value);
  999. r = send_cmd(h, AT_CMD_QMTCFG, NULL, buf, sizeof(buf));
  1000. if(r!=AT_OK) {
  1001. return -1;
  1002. }
  1003. return 0;
  1004. }
  1005. static void conn_init(cell_handle_t *h, mqtt_conn_t *conn)
  1006. {
  1007. char buf[100];
  1008. conn->h = h;
  1009. conn->ver = MQTT_VER;
  1010. conn->pdpId = conn->cliId+1; //1~15
  1011. conn->msgId = 0;
  1012. conn->qos = MQTT_QOS;
  1013. conn->retain = 1;
  1014. conn->will.flag = MQTT_WILL;
  1015. conn->will.qos = MQTT_QOS;
  1016. conn->will.retain = MQTT_RETAIN;
  1017. strcpy(conn->will.topic, "");
  1018. strcpy(conn->will.message, "");
  1019. conn->will.message_len = strlen(conn->will.message);
  1020. conn->timeout = 10; //1~60, default:5
  1021. conn->retry_times = 3;
  1022. conn->timeout_notice = 1;
  1023. conn->clean_session = 1;
  1024. conn->ssl_enable = 0;
  1025. conn->ssl_ctx_id = 0;
  1026. conn->msg_recv_mode = 0; //0: urc, 1: not urc
  1027. conn->msg_len_enable = 1;
  1028. conn->ping_interval = 5; //5~60, default:5
  1029. conn->proto_chk_enable = 0; //0:no check 1:check
  1030. conn->send_mode = 0; //0:string 1:hex
  1031. conn->recv_mode = 0; //0:string 1:hex
  1032. conn->view_mode = 0; //0:no echo 1:echo
  1033. conn->edit_mode = 0; //0:timeout not quit 1:timeout quit
  1034. conn->edit_time = 5; //1~120 seconds
  1035. //conn->plat.product_key[64]; //aliyun used
  1036. //conn->plat.device_name[64]; //aliyun used
  1037. //conn->plat.device_secret[64]; //aliyun/ used
  1038. //conn->plat.hw_time_enable; //huawei used
  1039. //conn->plat.product_secret[64]; //huawei used
  1040. //conn->plat.node_id[64];
  1041. //conn->plat.product_id[64]; //onenet used
  1042. //conn->plat.access_key[64]; //onenet used
  1043. sprintf(buf, "%d,%d", conn->cliId, conn->ver);
  1044. conn_cfg(conn, "version", buf);
  1045. sprintf(buf, "%d,%d", conn->cliId, conn->pdpId);
  1046. conn_cfg(conn, "pdpcid", buf);
  1047. sprintf(buf, "%d,%d", conn->cliId, conn->keep_alive_time);
  1048. conn_cfg(conn, "keepalive", buf);
  1049. sprintf(buf, "%d,%d", conn->cliId, conn->clean_session);
  1050. conn_cfg(conn, "session", buf);
  1051. sprintf(buf, "%d,%d,%d,%d", conn->cliId, conn->timeout, conn->retry_times, conn->timeout_notice);
  1052. conn_cfg(conn, "timeout", buf);
  1053. sprintf(buf, "%d,%d,%d", conn->cliId, conn->ssl_enable, conn->ssl_ctx_id);
  1054. conn_cfg(conn, "ssl", buf);
  1055. sprintf(buf, "%d,%d", conn->cliId, conn->ping_interval);
  1056. conn_cfg(conn, "qmtping", buf);
  1057. //sprintf(buf, "%d,%d,%d,%d,\"%s\",\"%s\"", conn->cliId, conn->will.flag, conn->will.qos, conn->will.retain, conn->will.topic, conn->will.message);
  1058. //conn_cfg(conn, "will", buf);
  1059. //sprintf(buf, "%d,%d,%d,%d,\"%s\",%d", conn->cliId, conn->will.flag, conn->will.qos, conn->will.retain, conn->will.topic, conn->will.message_len);
  1060. //conn_cfg(conn, "willex", buf);
  1061. sprintf(buf, "%d,%d", conn->cliId, conn->send_mode);
  1062. conn_cfg(conn, "send/mode", buf);
  1063. sprintf(buf, "%d,%d,%d", conn->cliId, conn->msg_recv_mode, conn->msg_len_enable);
  1064. conn_cfg(conn, "recv/mode", buf);
  1065. sprintf(buf, "%d,%d", conn->cliId, conn->view_mode);
  1066. conn_cfg(conn, "view/mode", buf);
  1067. sprintf(buf, "%d,%d,%d", conn->cliId, conn->edit_mode, conn->edit_time);
  1068. conn_cfg(conn, "edit/timeout", buf);
  1069. sprintf(buf, "%d,%d", conn->cliId, conn->proto_chk_enable);
  1070. conn_cfg(conn, "protocol/check", buf);
  1071. sprintf(buf, "%d,%d,%d", conn->cliId, conn->send_mode, conn->recv_mode);
  1072. conn_cfg(conn, "dataformat", buf);
  1073. #if 0 //aliyun
  1074. sprintf(buf, "%d,\"%s\",\"%s\",\"%s\"", conn->cliId, conn->plat.product_key, conn->plat.device_name, conn->plat.device_secret);
  1075. conn_cfg(conn, "aliauth", buf);
  1076. #endif
  1077. #if 0 //huawei
  1078. sprintf(buf, "%d,\"%s\",\"%s\"", conn->cliId, conn->plat.product_id, conn->plat.device_secret);
  1079. conn_cfg(conn, "hwauth", buf);
  1080. sprintf(buf, "%d,\"%s\",\"%s\",\"%s\"", conn->cliId, conn->plat.product_id, conn->plat.product_secret, conn->plat.node_id);
  1081. conn_cfg(conn, "hwprodid", buf);
  1082. #endif
  1083. #if 0 //onenet
  1084. sprintf(buf, "%d,\"%s\",\"%s\"", conn->cliId, conn->plat.product_id, conn->plat.access_key);
  1085. conn_cfg(conn, "onenet", buf);
  1086. #endif
  1087. }
  1088. static int cell_test(cell_handle_t *h)
  1089. {
  1090. int r;
  1091. cell_info_t info;
  1092. cell_stat_t stat;
  1093. cell_get_info(h, &info);
  1094. while(1) {
  1095. r = cell_get_stat(h, &stat);
  1096. if(r==0) {
  1097. LOGD("___dbm: %d ber: %d\n", stat.dbm, stat.ber);
  1098. }
  1099. else {
  1100. LOGE("___ get stat failed\n");
  1101. }
  1102. sleep(3);
  1103. }
  1104. return 0;
  1105. }
  1106. /////////////////////////////////////////////////////////////////////////
  1107. static int urc_handle(char *data, int len)
  1108. {
  1109. char *key=NULL;
  1110. key = "+QIURC: ";
  1111. if(strstr(data, key)) {
  1112. }
  1113. key = "+QMT";
  1114. if(strstr(data, key)) {
  1115. }
  1116. return 0;
  1117. }
  1118. static void* cellular_thread(void *arg)
  1119. {
  1120. int r;
  1121. uint32_t cnt=0,prelinkcnt=0;
  1122. char rxbuf[BUF_LEN];
  1123. thread_handle_t *th=(thread_handle_t*)arg;
  1124. cell_handle_t *h=(cell_handle_t*)th->arg;
  1125. port_init(h);
  1126. while (th->quit==0) {
  1127. cnt++;
  1128. if(h->pcell->mode) {
  1129. if (h->fd<0)
  1130. {
  1131. port_init(h);
  1132. }
  1133. if(cnt%5==0) {
  1134. cell_init(h);
  1135. }
  1136. if (cnt % 5 == 0)
  1137. {
  1138. // cell_get_info(h, &h->all.info);
  1139. cell_get_stat(h, &h->all.stat);
  1140. }
  1141. if (h->all.stat.linked == 1)
  1142. {
  1143. prelinkcnt = cnt;
  1144. }
  1145. if (cnt -prelinkcnt >=60 && h->all.stat.linked == 0)
  1146. {
  1147. prelinkcnt = cnt;
  1148. cell_reset(h);
  1149. }
  1150. }
  1151. else
  1152. {
  1153. if( h->fd>=0)
  1154. {
  1155. cell_deinit(h);
  1156. port_deinit(h);
  1157. }
  1158. reset_var(h);
  1159. }
  1160. UpdateCellularManager(&h->all);
  1161. sleep(1);
  1162. }
  1163. cell_deinit(h);
  1164. port_deinit(h);
  1165. pthread_exit(NULL);
  1166. }
  1167. int cellular_reset(void)
  1168. {
  1169. int r=-1;
  1170. cell_handle_t *h=&ceHandle;
  1171. r = cell_reset(h);
  1172. return r;
  1173. }
  1174. int cellular_init(void)
  1175. {
  1176. int r;
  1177. cell_handle_t *h=&ceHandle;
  1178. if (__globalDeviceManage._globalDevInfo.cell.Cellular_enable)
  1179. {
  1180. memset(h, 0, sizeof(cell_handle_t));
  1181. h->dm = &__globalDeviceManage;
  1182. h->pcell = &h->dm->_globalDevInfo.cell;
  1183. h->prod_id = h->dm->_globalDevInfo.product.id;
  1184. h->lck = lock_d_init();
  1185. nic_set(NIC_USB, 1);
  1186. thread_start(THREAD_ID_CELLULAR, cellular_thread, h);
  1187. }
  1188. return 0;
  1189. }
  1190. int cellular_deinit(void)
  1191. {
  1192. cell_handle_t *h=&ceHandle;
  1193. if (__globalDeviceManage._globalDevInfo.cell.Cellular_enable)
  1194. {
  1195. lock_d_deinit(h->lck);
  1196. thread_stop(THREAD_ID_CELLULAR);
  1197. }
  1198. return 0;
  1199. }
  1200. int cellular_get_all(cell_all_t *all)
  1201. {
  1202. int r=-1;
  1203. cell_handle_t *h=&ceHandle;
  1204. if(all) {
  1205. *all = h->all;
  1206. r = 0;
  1207. }
  1208. return r;
  1209. }
  1210. int cellular_is_connected(void)
  1211. {
  1212. int r=0;
  1213. cell_handle_t *h=&ceHandle;
  1214. if (__globalDeviceManage._globalDevInfo.cell.Cellular_enable)
  1215. {
  1216. r = h->all.stat.linked;
  1217. }
  1218. return r;
  1219. }
  1220. int UpdateCellularManager( cell_all_t *all)
  1221. {
  1222. int r = -1;
  1223. if (all)
  1224. {
  1225. __globalDeviceManage._globalDevInfo.cell.Link_status = all->stat.linked;
  1226. __globalDeviceManage._globalDevInfo.cell.Signal_strength = all->stat.dbm;
  1227. strcpy( __globalDeviceManage._globalDevInfo.cell.apn , all->info.apn);
  1228. strcpy( __globalDeviceManage._globalDevInfo.cell.Cellular_name , all->info.isp);
  1229. strcpy( __globalDeviceManage._globalDevInfo.cell.Cellular_Num , all->info.imei);
  1230. r=0;
  1231. }
  1232. return r;
  1233. }
  1234. int cellular_test(void)
  1235. {
  1236. cell_handle_t *h=&ceHandle;
  1237. cell_test(h);
  1238. return 0;
  1239. }