Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: modem_ref_helper_for_v5_3_217
Diff: modem_ref.cpp
- Revision:
- 13:10589aaf8853
- Parent:
- 11:a077da5fe68f
- Child:
- 14:a9c324663732
--- a/modem_ref.cpp Fri Aug 25 09:39:26 2017 +0000
+++ b/modem_ref.cpp Mon Sep 18 13:35:49 2017 +0000
@@ -2,11 +2,14 @@
#include "WizziDebug.h"
#include "modem_ref.h"
+#include "kal_crypto.h"
#if 0
#define REF_PRINT(...) PRINT(__VA_ARGS__)
+ #define URC_PRINT(...) PRINT(__VA_ARGS__)
#else
#define REF_PRINT(...);
+ #define URC_PRINT(...);
#endif
typedef struct {
@@ -34,6 +37,8 @@
#define STATE_OPEN 0xab
#define STATE_CLOSED 0
+#define ROOT_KEY_SIZE 16
+
#define SERIAL_SEND(_flow,_data,_size) do { \
u8 wch[WC_HEADER_SIZE] = {WC_SYNC_BYTE_0,WC_SYNC_BYTE_1,_size,g_modem.tx_sequ++,_flow};\
g_modem.send(wch,WC_HEADER_SIZE,_data,_size);\
@@ -68,13 +73,13 @@
int id = -1;
s8 err = ALP_ERR_NONE;
u8 eop;
- REF_PRINT("input 0x%x/%d Bytes\n",flowid,size);
+ //REF_PRINT("input 0x%x/%d Bytes\n",flowid,size);
switch (flowid)
{
case WC_FLOW_ALP_UNS:
case WC_FLOW_ALP_CMD:
rem -= alp_parse_chunk(&p, &r);
- REF_PRINT("Rem %d Bytes\n", rem);
+ //REF_PRINT("Rem %d Bytes\n", rem);
// This should always be a TAG'ed request in case of FS access...
if (r.type == ALP_OPCODE_TAG)
{
@@ -113,14 +118,12 @@
g_modem.istatus = (modem_istatus_t){0};
do
{
-
-
switch (r.type)
{
case ALP_OPCODE_RSP_ISTATUS:
// ISTATUS can come either alone or together with ALP_OPCODE_RSP_F_DATA
// but there should be only one per payload, moreover it will come first
- REF_PRINT("Got ISTATUS[%x] %d Bytes\n",r.meta.itf.type,r.meta.itf.length);
+ URC_PRINT("Got ISTATUS[%x] %d Bytes\n",r.meta.itf.type,r.meta.itf.length);
g_modem.istatus.type = r.meta.itf.type;
g_modem.istatus.length = r.meta.itf.length;
g_modem.istatus.data = r.data;
@@ -154,7 +157,7 @@
ASSERT(false,"ASSERT: Unsupported ALP URC: %d\n",r.type);
break;
}
-
+
int tem = alp_parse_chunk(&p, &r);
if (!tem)
{
@@ -162,7 +165,6 @@
}
rem -= tem;
} while (rem>=0);
-
}
break;
case WC_FLOW_ALP_RESP:
@@ -237,6 +239,23 @@
}
}
+private u32 modem_add_root_permission(u8* p, u8* root_key, u8* action, u32 action_size)
+{
+ u8 hash[32];
+
+ // Calculate hash on action
+ kal_sha256_init();
+ kal_sha256_update(action, action_size);
+ kal_sha256_update(root_key, ROOT_KEY_SIZE);
+ kal_sha256_final(hash);
+ // Prepend Permission-request...
+ ALP_ACTION_PERM_REQ(p, false, ALP_PERM_REQ_ROOT);
+ // ... and hash
+ memcpy(p, hash, ALP_WIZZILAB_AUTH_PROTOCOL_TOKEN_SIZE);
+
+ return ALP_ACTION_PERM_REQ_OFFSET;
+}
+
public int modem_get_id(action_callback_t* cb)
{
int i;
@@ -245,7 +264,7 @@
if (g_modem.user[i].cb == NULL)
{
g_modem.user[i].cb = cb;
- REF_PRINT("Get ID:%d/0x%08x\n",i,cb);
+ //REF_PRINT("Get ID:%d/0x%08x\n",i,cb);
return i;
}
}
@@ -260,7 +279,7 @@
g_modem.user[id].cb = NULL;
g_modem.user[id].data = NULL;
g_modem.user[id].istatus= NULL;
- REF_PRINT("Free ID:%d\n",id);
+ //REF_PRINT("Free ID:%d\n",id);
return id;
}
return -1;
@@ -293,7 +312,7 @@
SERIAL_SEND(WC_FLOW_SYS_RST,NULL,0);
}
-public void modem_read_file(u8 fid,void *data,u32 offset,u32 length, u8 id)
+public void modem_read_file(u8 fid, void *data, u32 offset, u32 length, u8 id)
{
u8 tmp[ALP_ACTION_TAG_SIZE + ALP_ACTION_F_RD_DATA_SIZE_MAX];
u8* p = tmp;
@@ -315,6 +334,53 @@
SERIAL_SEND(WC_FLOW_ALP_UNS,tmp, (u8)(p-tmp));
}
+public void modem_read_fprop_root(u8 fid, alp_file_header_t* data, u8* root_key, u8 id)
+{
+ u8 tmp[ALP_ACTION_TAG_SIZE + ALP_ACTION_PERM_REQ_SIZE(ALP_ACTION_F_RD_PROP_SIZE)];
+ u8* p = tmp;
+ u8* lastp;
+ REF_PRINT("RD PROPS (ROOT)[%d]\n",fid);
+ ASSERT(root_key != NULL, "Missing ROOT KEY\n");
+ g_modem.user[id].data = (u8*)data;
+ ALP_ACTION_TAG(p, id, true);
+ // Actual Operation will take place after PERM_REQ
+ p = &tmp[ALP_ACTION_TAG_SIZE + ALP_ACTION_PERM_REQ_OFFSET];
+ ALP_ACTION_F_RD_PROP(p,true,fid);
+ lastp = p;
+ modem_add_root_permission(&tmp[ALP_ACTION_TAG_SIZE], root_key,
+ &tmp[ALP_ACTION_TAG_SIZE + ALP_ACTION_PERM_REQ_OFFSET],
+ ALP_ACTION_F_RD_PROP_SIZE);
+ SERIAL_SEND(WC_FLOW_ALP_UNS,tmp, (u8)(lastp-tmp));
+}
+
+public void modem_write_fprop(u8 fid, alp_file_header_t* data, u8 id)
+{
+ u8 tmp[ALP_ACTION_TAG_SIZE + ALP_ACTION_F_WR_PROP_SIZE_MAX];
+ u8* p = tmp;
+ DPRINT(L_API, "WR PROPS[%d]\n",fid);
+ ALP_ACTION_TAG(p,id,true);
+ ALP_ACTION_F_WR_PROP(p,true,fid,0,sizeof(alp_file_header_t),data);
+ SERIAL_SEND(WC_FLOW_ALP_UNS,tmp, (u8)(p-tmp));
+}
+
+public void modem_write_fprop_root(u8 fid, alp_file_header_t* data, u8* root_key, u8 id)
+{
+ u8 tmp[ALP_ACTION_TAG_SIZE + ALP_ACTION_PERM_REQ_SIZE(ALP_ACTION_F_WR_PROP_SIZE_MAX)];
+ u8* p = tmp;
+ u8* lastp;
+ DPRINT(L_API, "WR PROPS (ROOT)[%d]\n",fid);
+ ASSERT(root_key != NULL, "Missing ROOT KEY\n");
+ ALP_ACTION_TAG(p, id, true);
+ // Actual Operation will take place after PERM_REQ
+ p = &tmp[ALP_ACTION_TAG_SIZE + ALP_ACTION_PERM_REQ_OFFSET];
+ ALP_ACTION_F_WR_PROP(p,true,fid,0,sizeof(alp_file_header_t),data);
+ lastp = p;
+ modem_add_root_permission(&tmp[ALP_ACTION_TAG_SIZE], root_key,
+ &tmp[ALP_ACTION_TAG_SIZE + ALP_ACTION_PERM_REQ_OFFSET],
+ ALP_ACTION_F_WR_PROP_SIZE_MAX);
+ SERIAL_SEND(WC_FLOW_ALP_UNS,tmp, (u8)(lastp-tmp));
+}
+
public void modem_write_file(u8 fid,void *data,u32 offset,u32 length, u8 id)
{
ALLOC_BUFFER(u8,tmp,(ALP_ACTION_TAG_SIZE + ALP_ACTION_F_WR_DATA_SIZE_MAX(length)));
@@ -327,6 +393,54 @@
DEALLOC_BUFFER(tmp);
}
+public void modem_write_file_root(u8 fid,void *data,u32 offset,u32 length, u8* root_key, u8 id)
+{
+ ALLOC_BUFFER(u8,tmp,(ALP_ACTION_TAG_SIZE + ALP_ACTION_PERM_REQ_SIZE(ALP_ACTION_F_WR_DATA_SIZE_MAX(length))));
+
+ u8* p = tmp;
+ u8* lastp;
+ REF_PRINT("WR (ROOT)[%d]@%d %d Bytes\n",fid,offset,length);
+ ASSERT(root_key != NULL, "Missing ROOT KEY\n");
+ ALP_ACTION_TAG(p, id, true);
+ // Actual Operation will take place after PERM_REQ
+ p = &tmp[ALP_ACTION_TAG_SIZE + ALP_ACTION_PERM_REQ_OFFSET];
+ ALP_ACTION_F_WR_DATA(p,true,fid,offset,length,data);
+ lastp = p;
+ modem_add_root_permission(&tmp[ALP_ACTION_TAG_SIZE], root_key,
+ &tmp[ALP_ACTION_TAG_SIZE + ALP_ACTION_PERM_REQ_OFFSET],
+ ALP_ACTION_F_WR_DATA_SIZE(offset,length));
+ SERIAL_SEND(WC_FLOW_ALP_UNS,tmp, (u8)(lastp-tmp));
+ DEALLOC_BUFFER(tmp);
+}
+
+public void modem_flush_file(u8 fid, u8 id)
+{
+ u8 tmp[ALP_ACTION_TAG_SIZE + ALP_ACTION_F_FLUSH_SIZE];
+ u8* p = tmp;
+ REF_PRINT("FLUSH[%d]\n",fid);
+ ALP_ACTION_TAG(p,id,true);
+ ALP_ACTION_F_FLUSH(p,true,fid);
+ SERIAL_SEND(WC_FLOW_ALP_UNS, tmp, (u8)(p-tmp));
+}
+
+public void modem_flush_file_root(u8 fid, u8* root_key, u8 id)
+{
+ u8 tmp[ALP_ACTION_TAG_SIZE + ALP_ACTION_PERM_REQ_SIZE(ALP_ACTION_F_FLUSH_SIZE)];
+ u8* p = tmp;
+ u8* lastp;
+ REF_PRINT("FLUSH (ROOT)[%d]\n",fid);
+ ASSERT(root_key != NULL, "Missing ROOT KEY\n");
+ ALP_ACTION_TAG(p, id, true);
+ // Actual Operation will take place after PERM_REQ
+ p = &tmp[ALP_ACTION_TAG_SIZE + ALP_ACTION_PERM_REQ_OFFSET];
+ ALP_ACTION_F_FLUSH(p,true,fid);
+ lastp = p;
+ modem_add_root_permission(&tmp[ALP_ACTION_TAG_SIZE], root_key,
+ &tmp[ALP_ACTION_TAG_SIZE + ALP_ACTION_PERM_REQ_OFFSET],
+ ALP_ACTION_F_FLUSH_SIZE);
+ SERIAL_SEND(WC_FLOW_ALP_UNS,tmp, (u8)(lastp-tmp));
+}
+
public void modem_declare_file(u8 fid, alp_file_header_t* hdr, u8 local, u8 id)
{
u8 tmp[ALP_ACTION_TAG_SIZE + ALP_ACTION_F_DECLARE_SIZE];
@@ -474,6 +588,4 @@
ALP_ACTION_RSP_F_DATA(p,fid,offset,length,data);
SERIAL_SEND(WC_FLOW_ALP_RESP,tmp, (u8)(p-tmp));
DEALLOC_BUFFER(tmp);
-}
-
-
+}
\ No newline at end of file