LoRaWAN demo.
Dependencies: modem_ref_helper DebouncedInterrupt
d7a_callbacks.cpp@20:49a8ecd1dda3, 2021-01-27 (annotated)
- Committer:
- Jeej
- Date:
- Wed Jan 27 14:45:50 2021 +0000
- Revision:
- 20:49a8ecd1dda3
- Child:
- 21:f0aecd41db08
Sanity commit. Do not use.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Jeej | 20:49a8ecd1dda3 | 1 | #include "modem_d7a.h" |
Jeej | 20:49a8ecd1dda3 | 2 | #include "ram_fs.h" |
Jeej | 20:49a8ecd1dda3 | 3 | |
Jeej | 20:49a8ecd1dda3 | 4 | |
Jeej | 20:49a8ecd1dda3 | 5 | // ============================================================}}} |
Jeej | 20:49a8ecd1dda3 | 6 | |
Jeej | 20:49a8ecd1dda3 | 7 | // Callbacks to MODEM's ALP requests |
Jeej | 20:49a8ecd1dda3 | 8 | // ============================================================{{{ |
Jeej | 20:49a8ecd1dda3 | 9 | void my_read(uint8_t fid, uint32_t offset, uint32_t length, int id) |
Jeej | 20:49a8ecd1dda3 | 10 | { |
Jeej | 20:49a8ecd1dda3 | 11 | uint8_t data[256]; |
Jeej | 20:49a8ecd1dda3 | 12 | #if 0 |
Jeej | 20:49a8ecd1dda3 | 13 | //if (!ram_fs_read(fid, (uint8_t*)data, offset, length)) |
Jeej | 20:49a8ecd1dda3 | 14 | { |
Jeej | 20:49a8ecd1dda3 | 15 | modem_ref_respond_read(fid, data, offset, length, id); |
Jeej | 20:49a8ecd1dda3 | 16 | } |
Jeej | 20:49a8ecd1dda3 | 17 | else |
Jeej | 20:49a8ecd1dda3 | 18 | #endif |
Jeej | 20:49a8ecd1dda3 | 19 | { |
Jeej | 20:49a8ecd1dda3 | 20 | modem_ref_respond(ALP_ERR_FILE_NOT_FOUND, id); |
Jeej | 20:49a8ecd1dda3 | 21 | } |
Jeej | 20:49a8ecd1dda3 | 22 | } |
Jeej | 20:49a8ecd1dda3 | 23 | |
Jeej | 20:49a8ecd1dda3 | 24 | void my_write(uint8_t fid, void *data, uint32_t offset, uint32_t length, int id) |
Jeej | 20:49a8ecd1dda3 | 25 | { |
Jeej | 20:49a8ecd1dda3 | 26 | if (!ram_fs_write(fid, (uint8_t*)data, offset, length)) |
Jeej | 20:49a8ecd1dda3 | 27 | { |
Jeej | 20:49a8ecd1dda3 | 28 | extern Queue<uint8_t, 8> g_file_modified; |
Jeej | 20:49a8ecd1dda3 | 29 | |
Jeej | 20:49a8ecd1dda3 | 30 | modem_ref_respond(ALP_ERR_NONE, id); |
Jeej | 20:49a8ecd1dda3 | 31 | g_file_modified.put((uint8_t*)(uint32_t)fid); |
Jeej | 20:49a8ecd1dda3 | 32 | } |
Jeej | 20:49a8ecd1dda3 | 33 | else |
Jeej | 20:49a8ecd1dda3 | 34 | { |
Jeej | 20:49a8ecd1dda3 | 35 | modem_ref_respond(ALP_ERR_FILE_NOT_FOUND, id); |
Jeej | 20:49a8ecd1dda3 | 36 | } |
Jeej | 20:49a8ecd1dda3 | 37 | } |
Jeej | 20:49a8ecd1dda3 | 38 | |
Jeej | 20:49a8ecd1dda3 | 39 | void my_read_fprop(uint8_t fid, int id) |
Jeej | 20:49a8ecd1dda3 | 40 | { |
Jeej | 20:49a8ecd1dda3 | 41 | uint8_t* hdr = (uint8_t*)ram_fs_get_header(fid); |
Jeej | 20:49a8ecd1dda3 | 42 | |
Jeej | 20:49a8ecd1dda3 | 43 | if (hdr != NULL) |
Jeej | 20:49a8ecd1dda3 | 44 | { |
Jeej | 20:49a8ecd1dda3 | 45 | modem_ref_respond_fprop(fid, (alp_file_header_t*)hdr, id); |
Jeej | 20:49a8ecd1dda3 | 46 | } |
Jeej | 20:49a8ecd1dda3 | 47 | else |
Jeej | 20:49a8ecd1dda3 | 48 | { |
Jeej | 20:49a8ecd1dda3 | 49 | modem_ref_respond(ALP_ERR_FILE_NOT_FOUND, id); |
Jeej | 20:49a8ecd1dda3 | 50 | } |
Jeej | 20:49a8ecd1dda3 | 51 | } |
Jeej | 20:49a8ecd1dda3 | 52 | |
Jeej | 20:49a8ecd1dda3 | 53 | void my_flush(uint8_t fid, int id) |
Jeej | 20:49a8ecd1dda3 | 54 | { |
Jeej | 20:49a8ecd1dda3 | 55 | // No flush in this file system |
Jeej | 20:49a8ecd1dda3 | 56 | modem_ref_respond(ALP_ERR_NONE, id); |
Jeej | 20:49a8ecd1dda3 | 57 | } |
Jeej | 20:49a8ecd1dda3 | 58 | |
Jeej | 20:49a8ecd1dda3 | 59 | void my_delete(uint8_t fid, int id) |
Jeej | 20:49a8ecd1dda3 | 60 | { |
Jeej | 20:49a8ecd1dda3 | 61 | modem_ref_respond((ram_fs_delete(fid))? ALP_ERR_FILE_NOT_FOUND : ALP_ERR_NONE, id); |
Jeej | 20:49a8ecd1dda3 | 62 | } |
Jeej | 20:49a8ecd1dda3 | 63 | |
Jeej | 20:49a8ecd1dda3 | 64 | void my_udata(void *data, uint32_t length) |
Jeej | 20:49a8ecd1dda3 | 65 | { |
Jeej | 20:49a8ecd1dda3 | 66 | uint8_t* p = (uint8_t*)data; |
Jeej | 20:49a8ecd1dda3 | 67 | int32_t rem = length; |
Jeej | 20:49a8ecd1dda3 | 68 | alp_parsed_chunk_t r; |
Jeej | 20:49a8ecd1dda3 | 69 | d7a_sp_res_t* istat; |
Jeej | 20:49a8ecd1dda3 | 70 | |
Jeej | 20:49a8ecd1dda3 | 71 | do { |
Jeej | 20:49a8ecd1dda3 | 72 | uint32_t parsed = alp_parse_chunk(&p, &r); |
Jeej | 20:49a8ecd1dda3 | 73 | if (!parsed) |
Jeej | 20:49a8ecd1dda3 | 74 | { |
Jeej | 20:49a8ecd1dda3 | 75 | // Discard the payload in case of parsing error. |
Jeej | 20:49a8ecd1dda3 | 76 | PRINT("Parsing error!\r\n"); |
Jeej | 20:49a8ecd1dda3 | 77 | break; |
Jeej | 20:49a8ecd1dda3 | 78 | } |
Jeej | 20:49a8ecd1dda3 | 79 | rem -= parsed; |
Jeej | 20:49a8ecd1dda3 | 80 | |
Jeej | 20:49a8ecd1dda3 | 81 | switch (r.type) |
Jeej | 20:49a8ecd1dda3 | 82 | { |
Jeej | 20:49a8ecd1dda3 | 83 | // Interface status |
Jeej | 20:49a8ecd1dda3 | 84 | case ALP_OPCODE_RSP_ISTATUS: |
Jeej | 20:49a8ecd1dda3 | 85 | // D7A Interface |
Jeej | 20:49a8ecd1dda3 | 86 | if (ALP_ITF_TYPE_D7A == r.meta.itf.type) |
Jeej | 20:49a8ecd1dda3 | 87 | { |
Jeej | 20:49a8ecd1dda3 | 88 | union { |
Jeej | 20:49a8ecd1dda3 | 89 | uint8_t b[8]; |
Jeej | 20:49a8ecd1dda3 | 90 | uint32_t w[2]; |
Jeej | 20:49a8ecd1dda3 | 91 | } uid; |
Jeej | 20:49a8ecd1dda3 | 92 | |
Jeej | 20:49a8ecd1dda3 | 93 | // ISTATUS can come either alone or together with ALP_OPCODE_RSP_F_DATA |
Jeej | 20:49a8ecd1dda3 | 94 | // but there should be only one per payload, moreover it will come first |
Jeej | 20:49a8ecd1dda3 | 95 | istat = (d7a_sp_res_t*)r.data; |
Jeej | 20:49a8ecd1dda3 | 96 | memcpy(uid.b,istat->addressee.id,8); |
Jeej | 20:49a8ecd1dda3 | 97 | |
Jeej | 20:49a8ecd1dda3 | 98 | PRINT("Got accessed by UID:%08X%08X SNR: %3ddB RXLEV: -%-3ddBm LB: %3ddB\n", |
Jeej | 20:49a8ecd1dda3 | 99 | HAL_U32_BYTE_SWAP(uid.w[0]), HAL_U32_BYTE_SWAP(uid.w[1]), |
Jeej | 20:49a8ecd1dda3 | 100 | istat->snr, istat->rxlev, istat->lb); |
Jeej | 20:49a8ecd1dda3 | 101 | } |
Jeej | 20:49a8ecd1dda3 | 102 | else |
Jeej | 20:49a8ecd1dda3 | 103 | { |
Jeej | 20:49a8ecd1dda3 | 104 | PRINT("Got accessed by unknown Interface 0x%02X\n", r.meta.itf.type); |
Jeej | 20:49a8ecd1dda3 | 105 | } |
Jeej | 20:49a8ecd1dda3 | 106 | break; |
Jeej | 20:49a8ecd1dda3 | 107 | // Data return |
Jeej | 20:49a8ecd1dda3 | 108 | case ALP_OPCODE_RSP_F_DATA: |
Jeej | 20:49a8ecd1dda3 | 109 | // RSP_F_DATA can come either alone or together with ISTATUS |
Jeej | 20:49a8ecd1dda3 | 110 | PRINT("Got UNS File[%3d]@%d %d Bytes\n", r.meta.f_data.fid, r.meta.f_data.offset, r.meta.f_data.length); |
Jeej | 20:49a8ecd1dda3 | 111 | break; |
Jeej | 20:49a8ecd1dda3 | 112 | default: |
Jeej | 20:49a8ecd1dda3 | 113 | PRINT("Untreated OPCODE %d\n", r.type); |
Jeej | 20:49a8ecd1dda3 | 114 | break; |
Jeej | 20:49a8ecd1dda3 | 115 | } |
Jeej | 20:49a8ecd1dda3 | 116 | } while (rem > 0); |
Jeej | 20:49a8ecd1dda3 | 117 | } |
Jeej | 20:49a8ecd1dda3 | 118 | |
Jeej | 20:49a8ecd1dda3 | 119 | void my_lqual(uint8_t ifid, int per) |
Jeej | 20:49a8ecd1dda3 | 120 | { |
Jeej | 20:49a8ecd1dda3 | 121 | PRINT("Interface File [%3d] LQUAL : %d%% PER\r\n", ifid, per); |
Jeej | 20:49a8ecd1dda3 | 122 | } |
Jeej | 20:49a8ecd1dda3 | 123 | |
Jeej | 20:49a8ecd1dda3 | 124 | void my_ldown(uint8_t ifid) |
Jeej | 20:49a8ecd1dda3 | 125 | { |
Jeej | 20:49a8ecd1dda3 | 126 | PRINT("Interface File [%3d] LDOWN\r\n", ifid); |
Jeej | 20:49a8ecd1dda3 | 127 | } |
Jeej | 20:49a8ecd1dda3 | 128 | |
Jeej | 20:49a8ecd1dda3 | 129 | void my_reset(void) |
Jeej | 20:49a8ecd1dda3 | 130 | { |
Jeej | 20:49a8ecd1dda3 | 131 | PRINT("Restarting application...\r\n"); |
Jeej | 20:49a8ecd1dda3 | 132 | FLUSH(); |
Jeej | 20:49a8ecd1dda3 | 133 | NVIC_SystemReset(); |
Jeej | 20:49a8ecd1dda3 | 134 | } |
Jeej | 20:49a8ecd1dda3 | 135 | |
Jeej | 20:49a8ecd1dda3 | 136 | void my_boot(uint8_t cause, u16 number) |
Jeej | 20:49a8ecd1dda3 | 137 | { |
Jeej | 20:49a8ecd1dda3 | 138 | PRINT("Modem BOOT[%c] #%d\r\n", cause, number); |
Jeej | 20:49a8ecd1dda3 | 139 | |
Jeej | 20:49a8ecd1dda3 | 140 | // Modem re-booted, restart APP |
Jeej | 20:49a8ecd1dda3 | 141 | my_reset(); |
Jeej | 20:49a8ecd1dda3 | 142 | } |
Jeej | 20:49a8ecd1dda3 | 143 | |
Jeej | 20:49a8ecd1dda3 | 144 | void my_busy(uint8_t busy) |
Jeej | 20:49a8ecd1dda3 | 145 | { |
Jeej | 20:49a8ecd1dda3 | 146 | if (busy) |
Jeej | 20:49a8ecd1dda3 | 147 | { |
Jeej | 20:49a8ecd1dda3 | 148 | PRINT("Modem Busy\r\n"); |
Jeej | 20:49a8ecd1dda3 | 149 | |
Jeej | 20:49a8ecd1dda3 | 150 | /* Stop report, do not use modem */ |
Jeej | 20:49a8ecd1dda3 | 151 | /* Wait for modem reboot or modem not busy */ |
Jeej | 20:49a8ecd1dda3 | 152 | } |
Jeej | 20:49a8ecd1dda3 | 153 | else |
Jeej | 20:49a8ecd1dda3 | 154 | { |
Jeej | 20:49a8ecd1dda3 | 155 | PRINT("Modem not Busy\r\n"); |
Jeej | 20:49a8ecd1dda3 | 156 | |
Jeej | 20:49a8ecd1dda3 | 157 | /* Resume reports */ |
Jeej | 20:49a8ecd1dda3 | 158 | } |
Jeej | 20:49a8ecd1dda3 | 159 | } |
Jeej | 20:49a8ecd1dda3 | 160 | |
Jeej | 20:49a8ecd1dda3 | 161 | void my_itf_busy(uint8_t ifid, uint32_t seconds) |
Jeej | 20:49a8ecd1dda3 | 162 | { |
Jeej | 20:49a8ecd1dda3 | 163 | extern Queue<uint32_t, 8> g_urc; |
Jeej | 20:49a8ecd1dda3 | 164 | |
Jeej | 20:49a8ecd1dda3 | 165 | //PRINT("ITF[%d] Busy for %d seconds.\r\n", ifid, seconds); |
Jeej | 20:49a8ecd1dda3 | 166 | g_urc.put((uint32_t*)seconds); |
Jeej | 20:49a8ecd1dda3 | 167 | } |