Sending and reception of big data file (1kB example)

Dependencies:   modem_ref_helper CRC DebouncedInterrupt

Committer:
Jeej
Date:
Thu May 28 09:08:51 2020 +0000
Revision:
14:a0241090f967
Parent:
11:dc0e19b2d4a0
Updated for v5.3.283.

Who changed what in which revision?

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