Cellular.c 43 KB

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