WizziCom Modem command handling library.
Revision 0:e3f3d9ec19fb, committed 2017-05-09
- Comitter:
- Jeej
- Date:
- Tue May 09 13:47:00 2017 +0000
- Commit message:
- WizziCom Modem command handling library.
Changed in this revision
WizziComModem.cpp | Show annotated file Show diff for this revision Revisions of this file |
WizziComModem.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r 000000000000 -r e3f3d9ec19fb WizziComModem.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/WizziComModem.cpp Tue May 09 13:47:00 2017 +0000 @@ -0,0 +1,142 @@ +#include "WizziComModem.h" + +//====================================================================== +// MODEM commands over COM port +//====================================================================== +typedef enum +{ //============================================ + // COMMANDS + //============================================ + // Soft-reset Modem Device + // ARGS: none + WM_CMD_RST = 0x01, + + // Start D7A communication stack + // ARGS: none TODO: pass number of fifos ? + WM_CMD_D7A_STACK_START = 0x02, + + // Stop D7A communication stack + // ARGS: none + WM_CMD_D7A_STACK_STOP = 0x03, + + // Register a File for notification + // ARGS: register_file_param_t + WM_CMD_REGISTER_FILE = 0x04, + + // Notify a (previously registered) file + // ARGS: notify_file_param_t + WM_CMD_NOTIFY_FILE = 0x05, + + // Command that will be instantly answered with WM_OK + // ARGS: none + WM_CMD_READY = 0x06, + + //============================================ + // RESPONSES + //============================================ + // Send by the Modem Device after a command has been executed/taken + // into account, and the Modem is ready to handle a new command. + // ARGS: none + WM_OK = 0x80, + + // Send by the Modem Device after a (re)boot. + // ARGS: boot_status_t + WM_BOOT = 0x81, + + // Send by the Modem Device in case of failing command + // ARGS: wm_error_code_t (u8) + WM_ERROR = 0x82, + + // Send by the Modem Device upon the end of a File notification. + // This response is generated depending on ALP's Retry-Policy ('respond' field set) + // ARGS: wm_notif_result_t + WM_NOTIF_DONE = 0x83, +} wm_cmd_t; + + +WizziComModem::WizziComModem(WizziCom* com, PinName reset_pin) : +_com(com), +_reset_pin(reset_pin), +_modem_ready(0) +{ + _com->attach(this, &WizziComModem::_modem_command, WizziComPacketCmd); +} + +WizziComModem::~WizziComModem() +{} + +void WizziComModem::_modem_command(WizziCom* com, WizziComPacket_t* pkt) +{ + uint8_t cmd = pkt->data[0]; + + if (cmd == WM_OK) + { + PRINT("Modem ready\r\n"); + _modem_ready.release(); + } + else if (cmd == WM_BOOT) + { + boot_status_t* bs = (boot_status_t*)&(pkt->data[1]); + if (_boot_callback) + { + _boot_callback.call(*bs); + } + //WARNING(false, "Modem booted CAUSE:%d NB_BOOT:%d\r\n", bs->bf.cause, bs->bf.nb_boot); + } + else if (cmd == WM_ERROR) + { + if (_error_callback) + { + _error_callback.call((int8_t)pkt->data[1]); + } + } + else if (cmd == WM_NOTIF_DONE) + { + PRINT("Notif done fid %d err %d\r\n", pkt->data[1], pkt->data[2]); + } + else + { + EPRINT("MODEM Unknown cmd %d\r\n", cmd); + } + + FREE(pkt); +} + +void WizziComModem::start_dash7(void) +{ + uint8_t cmd[] = { WM_CMD_D7A_STACK_START }; + _com->send(WizziComPacketCmd, sizeof(cmd), cmd); + _modem_ready.wait(); +} + +void WizziComModem::stop_dash7(void) +{ + uint8_t cmd[] = { WM_CMD_D7A_STACK_STOP }; + _com->send(WizziComPacketCmd, sizeof(cmd), cmd); + _modem_ready.wait(); +} + +void WizziComModem::software_reset(void) +{ + uint8_t cmd[] = { WM_CMD_RST }; + _com->send(WizziComPacketCmd, sizeof(cmd), cmd); + _modem_ready.wait(); +} + +void WizziComModem::hardware_reset(void) +{ + DigitalOut reset_low(_reset_pin, 0); + Thread::wait(100); + DigitalIn reset_release(_reset_pin); + _modem_ready.wait(); +} + +void WizziComModem::attach_boot(WizziComModemBootCallback function) +{ + _boot_callback = callback(function); +} + +void WizziComModem::attach_error(WizziComModemErrorCallback function) +{ + _error_callback = callback(function); +} \ No newline at end of file
diff -r 000000000000 -r e3f3d9ec19fb WizziComModem.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/WizziComModem.h Tue May 09 13:47:00 2017 +0000 @@ -0,0 +1,99 @@ +#ifndef _WIZZI_MODEM_H_ +#define _WIZZI_MODEM_H_ + +#include "mbed.h" +#include "rtos.h" +#include "WizziCom.h" +#include "WizziDebug.h" + +#define TYPEDEF_STRUCT_PACKED typedef struct __attribute__((packed)) + + +typedef enum { + WM_ERR_NONE = 0, + WM_ERR_NOT_READY = 1, + WM_ERR_COM_LINK = 2, + WM_ERR_ILLEGAL_FID = 3, + WM_ERR_ILLEGAL_FILE_DEF = 4, + + WM_ERR_UNKNOWN = 0xff +} wm_error_code_t; + +typedef union { + uint32_t w; + struct { + uint32_t cause : 8; + uint32_t rfu : 8; + uint32_t nb_boot :16; + } bf; +} boot_status_t; + +typedef struct { + union { + uint8_t b[8]; + uint32_t w[2]; + } uid; + uint8_t calib; +} wm_status_t; + + +TYPEDEF_STRUCT_PACKED { + uint8_t fid; + uint8_t type; + uint8_t afid; + uint8_t ifid; + uint8_t prop; + uint8_t perm; + uint32_t size; + uint32_t alloc; +} register_file_param_t; + +typedef union { + uint32_t w; + struct { + uint32_t fid : 8; + uint32_t offset : 12; + uint32_t size : 12; + } bf; +} notify_file_param_t; + +class WizziComModem { + typedef void (*WizziComModemBootCallback)(boot_status_t); + typedef void (*WizziComModemErrorCallback)(int8_t); + +private: + WizziCom* _com; + PinName _reset_pin; + Semaphore _modem_ready; + Callback<void(boot_status_t)> _boot_callback; + Callback<void(int8_t)> _error_callback; + + void _modem_command(WizziCom* com, WizziComPacket_t* pkt); + +public: + WizziComModem(WizziCom* com, PinName reset_pin = NC); + ~WizziComModem(); + + void attach_boot(WizziComModemBootCallback function); + + template<class T> + void attach_boot(T* object, void (T::*member)(boot_status_t)) + { + _boot_callback = callback(object, member); + } + + void attach_error(WizziComModemErrorCallback function); + + template<class T> + void attach_error(T* object, void (T::*member)(boot_status_t)) + { + _error_callback = callback(object, member); + } + + void start_dash7(void); + void stop_dash7(void); + void software_reset(void); + void hardware_reset(void); +}; + +#endif \ No newline at end of file