LM005A Lora Module test
Dependents: LoRaTest OnenetTest
Revision 1:0cad46274201, committed 2018-07-31
- Comitter:
- yao6116601
- Date:
- Tue Jul 31 02:55:28 2018 +0000
- Parent:
- 0:5b5a1b5f1ae4
- Commit message:
- BC95B -- For China Mobile oneNet IoT
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/BC95B/BC95B.cpp Tue Jul 31 02:55:28 2018 +0000 @@ -0,0 +1,287 @@ +/* BC95B oneNet Application +* reference document:Quectel_BC95_OneNET_Application_Note_V1.2.pdf +* By modular-2 team 2018/7/26 +*/ + +#include "BC95B.h" + +#define BC95B_DEFAULT_BAUD_RATE 9600 +#define READ 0 +#define WRITE 1 +#define DISCOVER 2 +BC95B::BC95B(PinName tx, PinName rx, bool debug) + : _serial(tx, rx, BC95B_DEFAULT_BAUD_RATE), + _parser(&_serial), + _fail(false) +{ + _serial.set_baud( BC95B_DEFAULT_BAUD_RATE ); + _parser.debug_on(debug); + _parser.set_delimiter("\r\n"); + _parser.oob("+MIPLWRITE:", callback(this, &BC95B::_write_handler)); + _parser.oob("+MIPLREAD:", callback(this, &BC95B::_read_handler)); + _parser.oob("+MIPLOBSERVE:", callback(this, &BC95B::_obs_handler)); + _parser.oob("+MIPLDISCOVER:", callback(this, &BC95B::_discover_handler)); +} + +const char * BC95B::get_firmware_version() +{ + if (!(_parser.send("AT+MIPLVER?") + && _parser.recv("+MIPLVER:%s\n",buffer) + && _parser.recv("OK"))) { + return NULL; + } + return buffer; + +} +const char * BC95B::get_imei() +{ + if (!(_parser.send("AT+CGSN=1") + && _parser.recv("+CGSN:%s\n",buffer) + && _parser.recv("OK"))) { + return NULL; + } + return buffer; + +} +const char * BC95B::get_imsi() +{ + if (!(_parser.send("AT+CIMI") + && _parser.recv("%s\n",buffer) + && _parser.recv("OK"))) { + return NULL; + } + return buffer; + +} +bool BC95B::get_SignalStrength(int* rssi,int* ber) +{ + if (!(_parser.send("AT+CSQ") + && _parser.recv("+CSQ:%d,%d\n",rssi,ber) + && _parser.recv("OK"))) { + return false; + } + return true; + +} +int BC95B::create_suite() +{ int result; + if (!(_parser.send("AT+MIPLCREATE") + && _parser.recv("+MIPLCREATE:%d",&result) + && _parser.recv("OK"))) { + return -1; + } + return result; + +} +bool BC95B::create_object(int ch,const int name,int insCount,const char * bitmap,int attrCount,int actCount) +{ + + if (!(_parser.send("AT+MIPLADDOBJ=%d,%d,%d,\"%s\",%d,%d",ch,name,insCount,bitmap,attrCount,actCount) + && _parser.recv("OK"))) { + return false; + } + return true; + } +bool BC95B::register_request(int ref,int lifetime,int timeout) + { + if (!(_parser.send("AT+MIPLOPEN=%d,%d,%d",ref,lifetime,timeout) + && _parser.recv("OK") + && _parser.recv("+MIPLEVENT: 0,1") + && _parser.recv("+MIPLEVENT: 0,2") + && _parser.recv("+MIPLEVENT: 0,4") + && _parser.recv("+MIPLEVENT: 0,6"))) + { + return false; + } + return true; + } +/*bool BC95B::respond_discover(int ref,int result ,int length,char *valuestring) +{ + if (!(_parser.send("AT+MIPLDISCOVERRSP=%d,%s,%d,%d,%S",ref,msg,result,length,valuestring) + && _parser.recv("OK"))){ + return false; + } + return true; + } */ +bool BC95B::delete_object(int ref,const char * objid) +{ + if (!(_parser.send("AT+MIPLDELOBJ=%d,%s",ref,objid) + && _parser.recv("OK"))){ + return false; + } + return true; + } +bool BC95B::deregister(int ref) +{ + if (!(_parser.send("AT+MIPLCLOSE=%d",ref) + && _parser.recv("OK"))){ + return false; + } + return true; + } +bool BC95B::notify_resource(int ref,int objid,int ins,int resid,int valueType,int len,const char * value,int index,int flg) +{ +if (!(_parser.send("AT+MIPLNOTIFY=%d,%d,%d,%d,%d,%d,%d,%s,%d,%d", ref,obsmsg,objid,ins,resid,valueType,len,value,index,flg) + && _parser.recv("OK"))){ + return false; + } + return true; + } + +bool BC95B::init(void) +{ + if (!_parser.recv("Bootup Completed!!!")) + return false; + return true; + } +bool BC95B::reset(void) +{ + if (!(_parser.send("AT+NRB") + &&_parser.recv("REBOOTING"))) { + return false; + } + return true; +} + + + + +/* ++MIPLWRITE--平台下发的写指令 ++MIPLWRITE: <ref>,<msgId>,<objI d>,<insId>,<resId>,<valueType>,<len>,<value>,<flag>,<index> +response: +AT+MIPLWRITERSP=<ref>,<msgId>,< result> +*/ + + +void BC95B::_write_handler() +{ + if (!(_parser.recv("%d,%d,%d,%d,%d,%d,%d,",&ref,&msg,&objId,&insId,&resId,&valueType,&len) + && _parser.read(value,len*2) + &&_parser.recv(",%d,%d\n",&flag,&index))) { + return; + } + + // printf("v=%.*s\n",len*2,value); + currCode=WRITE; + curr_objId=objId; + curr_insId=insId; + curr_resId=resId; + valueLength=len*2; + _callback(); + result=2; + if (! _parser.send("AT+MIPLWRITERSP=%d,%d,%d",ref,msg,result)) + { + return; + } + + } + /* + +MIPLREAD--平台下发的读指令 + +MIPLREAD: <ref>,<msgId>,<objId>,<insId>, <resId> + response: + AT+MIPLREADRSP=<ref>,<msgId>, <result>[,<objId>,<insId>,<resId>,< valueType>,<len>,<value>,<index>, <flag>] + */ + void BC95B::_read_handler() + { int valueType,len,index,flg; + if (!_parser.recv("%d,%d,%d,%d,%d\n",&ref,&msg,&objId,&insId,&resId)) + { + return ; + } + //getValue(objId,insId,resId); + //"AT+MIPLREADRSP=%d,%s",ref,msgId,result,objId,insId,resId,valueType,len,value,index,flg) + currCode=READ; + curr_objId=objId; + curr_insId=insId; + curr_resId=resId; + _callback(); + len=4;index=0;flg=0;result=1;valueType=currType; + // memcpy(value,"3.8",3); + if (! _parser.send("AT+MIPLREADRSP=%d,%d,%d,%d,%d,%d,%d,%d,%s,%d,%d ",ref,msg,result,objId,insId,resId,valueType,len,value,index,flg)) + { + return; + } + } +/* ++MIPLOBSERVE: <ref>,<msgId>,<flag>,<objI d>,<insId>,<resId> +AT+MIPLDISCOVERRSP=<ref>,<msgI d>,<result>[,<length>,<valuestring>] +*/ +void BC95B::_obs_handler() +{ + if (!_parser.recv("%d,%d,%d,%d,%d,%d\n",&ref,&obsmsg,&flag,&objId,&insId,&resId)) { + return; + } + result=1; + if (! _parser.send("AT+MIPLOBSERVERSP=%d,%d,%d",ref,obsmsg,result)) + { + return; + } + } +void BC95B::_discover_handler() +{ + if (!_parser.recv("%d,%d,%d\n",&ref,&msg,&objId)) { + return; + } + // printf("discover\n"); + currCode=DISCOVER; + curr_objId=objId; + _callback(); + result=1; + if (! _parser.send("AT+MIPLDISCOVERRSP=0,%d,%d,%d,\"%s\"",msg,result,resLength,resNames)) + { + return; + } + } +bool BC95B::recv() +{ + if (!_parser.process_oob()) { + return false; + } + return true; + } + +void BC95B::setTimeout(uint32_t timeout_ms) +{ + _parser.set_timeout(timeout_ms); +} + +void BC95B::attach(void (*func)(void)) +{ + _callback=func; + + } + + int BC95B::getEventCode() + { + return currCode; + } + ; + void BC95B::getCurrentID(long *msgId,int *objId,int *insId,int *resId) + { + *msgId=curr_msgId; + *objId=curr_objId; + *insId=curr_insId; + *resId=curr_resId; + }; + int BC95B::getCurrentValue(char *buf) + { + memcpy(buf,value,valueLength); + return valueLength; + }; + int BC95B::putCurrentValue(int type,char *buf,int length) + { + valueLength= length ; + currType=type; + memcpy(value,buf,valueLength); + return valueLength; + }; + + int BC95B::putCurrentResNames(int type,char *buf,int length) + { + printf("reslength=%d\n",length); + resLength= length ; + currType=type; + memcpy(resNames,buf,resLength); + return resLength; + }; +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/BC95B/BC95B.h Tue Jul 31 02:55:28 2018 +0000 @@ -0,0 +1,59 @@ +/* BC95BInterface Example + + */ + +#ifndef BC95B_H +#define BC95B_H + +#include "ATCmdParser.h" + +/** BC95BInterface class. + This is an interface to a BC95B radio. + */ +class BC95B +{ +protected: +void (*_callback)(void); +public: + BC95B(PinName tx, PinName rx, bool debug=false); + bool init(void); + const char * get_firmware_version(void); + const char * get_imei(); + const char * get_imsi(); + bool BC95B::get_SignalStrength (int* rssi,int* ber); + int create_suite(); + bool create_object(int ch,const int name,int insCount,const char * bitmap,int attrCount,int actCount); + bool register_request(int ref,int lifetime,int timeout); + //bool respond_discover(int ref,int result ,int length,char *valuestring); + bool delete_object(int ref,const char * objid); + bool deregister(int ref); + bool notify_resource(int ref,int objid,int ins,int resid,int valueType,int len,const char * value,int index,int flg); + bool reset(void); + void attach(void (*func)(void)); + bool recv(void); + void setTimeout(uint32_t timeout_ms); + int getEventCode(); + void getCurrentID(long *msgId,int *objID,int *insId,int *resId); + int getCurrentValue(char *buf); + int putCurrentValue(int type,char *buf,int length); + int putCurrentResNames(int type,char *buf,int length); +private: + int ref,objId,insId,resId,valueType,len,index,flag,result; + int curr_objId,curr_insId,curr_resId,currCode,currType; + long curr_msgId; + long msg,obsmsg; + char value[32]; + char resNames[32]; + int valueLength; + int resLength; + UARTSerial _serial; + ATCmdParser _parser; + char buffer[64]; + bool _fail; + void _read_handler(); + void _write_handler(); + void _obs_handler(); + void _discover_handler(); +}; + +#endif
--- a/LM005A/LM005A.cpp Thu Apr 19 13:15:25 2018 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,237 +0,0 @@ -/* LM005A Example - * Copyright (c) 2015 ARM Limited - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "LM005A.h" -#include <cstring> - -#define LM005A_DEFAULT_BAUD_RATE 115200 - -LM005A::LM005A(PinName tx, PinName rx, bool debug) - : _serial(tx, rx, LM005A_DEFAULT_BAUD_RATE), - _parser(&_serial), - _fail(false) -{ - _serial.set_baud( LM005A_DEFAULT_BAUD_RATE ); - _parser.debug_on(debug); - _parser.set_delimiter("\r\n"); - _parser.oob("+RXDONE:", callback(this, &LM005A::_packet_handler)); - // _parser.oob("+JOIN:", callback(this, &LM005A::_tx_handler)); - -} - -const char * LM005A::get_firmware_version() -{ - if (!(_parser.send("AT+VERSION?") - && _parser.recv("+VERSION:%s\n",buffer) - && _parser.recv("OK"))) { - return NULL; - } - return buffer; - -} -bool LM005A::init(void) -{ - if (!_parser.recv("Bootup Completed!!!")) - return false; - return true; - } -bool LM005A::reset(void) -{ - if (!(_parser.send("AT+RESET") - && _parser.recv("+RESET") - && _parser.recv("OK") - &&_parser.recv("Bootup Completed!!!"))) { - return false; - } - return true; -} - - int LM005A::set_mode(int mode) - { - int res; - if (!(_parser.send("AT+MODE=%d",mode) - && _parser.recv("+MODE:%d", &res) - && _parser.recv("OK"))) { - return -1; - } - - return res; - } -int LM005A::set_class(int class_mode) -{ - int res; - if (!(_parser.send("AT+CLASS=%d",class_mode) - && _parser.recv("+CLASS:%d", &res) - && _parser.recv("OK"))) { - return -1; - } - - return res; - } -int LM005A::set_band(int gap,int band) -{ - int g,f; - if (!(_parser.send("AT+FIXEDBAND=%d,%d",gap,band) - && _parser.recv("+FIXEDBAND:%d,%d\n",&g ,&f) - && _parser.recv("OK"))) { - return 0; - } - - return f; } -int LM005A::set_power(int power) - { - int res; - if (!(_parser.send("AT+POWER=%d",power) - && _parser.recv("+POWER:%d\n", &res) - && _parser.recv("OK"))) { - return 0; - } - - return res; - } - -bool LM005A::join() -{ - - if (!(_parser.send("AT+JOIN") - && _parser.recv("OK"))) { - return false; - } - return true; -} - - const char *LM005A::getEUI(int type) - { - if (type==0) - { if (!(_parser.send("AT+DEVEUI?") - && _parser.recv("+DEVEUI%s\n", buffer) - && _parser.recv("OK"))) { - return NULL; - } - } - else - { - if (!(_parser.send("AT+APPEUI?") - && _parser.recv("+APPEUI%s\n", buffer) - && _parser.recv("OK"))) { - return NULL; - } - } - return buffer; -} -const char *LM005A::getDeviceAddress(void) -{ - - - if (!(_parser.send("AT+DEVADDR?") - && _parser.recv("+DEVADDR:%s\n", buffer) - && _parser.recv("OK"))) { - return NULL; - } - - return buffer; -} - -unsigned char hex_char(char c) -{ - if ('0' <= c && c <= '9') return (unsigned char)(c - '0'); - if ('A' <= c && c <= 'F') return (unsigned char)(c - 'A' + 10); - if ('a' <= c && c <= 'f') return (unsigned char)(c - 'a' + 10); - // printf("Error Val=%d\n",c); - return 0xFF; -} -bool LM005A::send(int port,int ack, const void *data, uint32_t amount) -{ - - if (!(_parser.send("AT+MSG=%d,%d,%d,%.*s",port,ack,amount,amount,data) - && _parser.recv("+MSG:TXDONE") - && _parser.recv("+MSG:RXDONE") - && _parser.recv("OK"))) - return false; - return true; -} -bool LM005A::sendhex(int port,int ack, const void *data, uint32_t amount) -{ - - if (!(_parser.send("AT+MSGHEX=%d,%d,%d,%.*s",port,ack,amount,amount,data) - && _parser.recv("+MSGHEX:TXDONE") - && _parser.recv("OK"))) - return false; - return true; -} - -void LM005A::_packet_handler() -{ uint32_t amount; -char rBuf[128]; -int i,p; -char val; - if (!_parser.recv("%d,",&amount)) { - return; - } - printf("amount=%d\n",amount); - rCounter=rCounter+amount; - _parser.read(rBuf,amount*3-1); - p=0; - for (i=0;i<amount;i++) - { - val=hex_char( rBuf[p++]); - val=(val<<4)+hex_char( rBuf[p++]); - p++; - rBuffer[rIndex++]=val; - } - rBuffer[rIndex++]=0x00; - // printf("Rec=%0.*s",amount,rBuffer); - - } - - -int32_t LM005A::recv(int id, void *data, uint32_t amount) -{ - rIndex=0; -rCounter=0; - - while (true) { - if (rCounter>0) - { - memcpy(data,rBuffer,rCounter); - break; - - } - if (!_parser.process_oob()) { - return -1; - } - } - return rCounter; -} - - -void LM005A::setTimeout(uint32_t timeout_ms) -{ - _parser.set_timeout(timeout_ms); -} - - - -void LM005A::attach(Callback<void()> func) -{ - _serial.sigio(func); -} - - - - - -
--- a/LM005A/LM005A.h Thu Apr 19 13:15:25 2018 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,161 +0,0 @@ -/* LM005AInterface Example - * Copyright (c) 2015 MAXIUM Limited - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef LM005A_H -#define LM005A_H - -#include "ATCmdParser.h" - -/** LM005AInterface class. - This is an interface to a LM005A radio. - */ -class LM005A -{ -public: - LM005A(PinName tx, PinName rx, bool debug=false); - bool init(void); - /** - * Check firmware version of LM005A - * - * @return firmware version string - */ - const char * get_firmware_version(void); - - /** - * 设置终端入网激活模式 - * - * @param mode 终端入网激活模式 0-OTA, 1-ABP - * @return true only if LM005A was setup correctly - */ - int set_mode(int mode); - /** - * 设置终端CLASS模式 - * - * @param class_mode 终端入网CLASS模式 0-OTA, 1-ABP - * @return true only if LM005A was setup correctly - */ - int set_band(int gap,int band); - /** - * 设置终端CLASS模式 - * - * @param class_mode 终端入网CLASS模式 0-OTA, 1-ABP - * @return true only if LM005A was setup correctly - */ - int set_class(int class_mode); - /** - * 设置终端发射功率 - * - * @param power 终端终端发射功率 (20,17,16,14,12,10,7,5,2) In DB - * @return true - */ -int set_power(int power); - /** - * Get EUI - * @param type 0 deveui 1 appeui - * @return null-teriminated EUI or null if no IP address is assigned - */ - const char *getEUI(int type); - - /** - * Get device address - * eui - * @return null-teriminated Device address or null if no IP address is assigned - */ - const char *getDeviceAddress(void); - /** - * Reset LM005A - * - * @return true only if LM005A resets successfully - */ - - bool reset(void); - - /** - * jion LM005A to Network - * - * @param ap the name of the AP - * @param passPhrase the password of AP - * @return NSAPI_ERROR_OK only if LM005A is connected successfully - */ - bool join(); - - /** - * Send data - * - * @param port 端口 ack 重发标志 - * @param data data to be sent - * @param amount amount of data to be sent - max 1024 - * @return true only if data sent successfully - */ - bool send(int port,int ack, const void *data, uint32_t amount); - /** - * Send data hex - * - * @param port 端口 ack 重发标志 - * @param data data to be sent - * @param amount amount of data to be sent in hex - * @return true only if data sent successfully - */ - bool sendhex(int port,int ack, const void *data, uint32_t amount); - /** - * Receives data from an open socket - * - * @param id id to receive from - * @param data placeholder for returned information - * @param amount number of bytes to be received - * @return the number of bytes received - */ - int32_t recv(int id, void *data, uint32_t amount); - - /** - * Allows timeout to be changed between commands - * - * @param timeout_ms timeout of the connection - */ - void setTimeout(uint32_t timeout_ms); - - - /** - * Attach a function to call whenever network state has changed - * - * @param func A pointer to a void function, or 0 to set as none - */ - void attach(Callback<void()> func); - - /** - * Attach a function to call whenever network state has changed - * - * @param obj pointer to the object to call the member function on - * @param method pointer to the member function to call - */ - template <typename T, typename M> - void attach(T *obj, M method) { - attach(Callback<void()>(obj, method)); - } - -private: - UARTSerial _serial; - ATCmdParser _parser; - char buffer[256]; - char rBuffer[256]; - int rIndex; - int rCounter; - int _connect_error; - bool _fail; - void _packet_handler(); -}; - -#endif