Skip to content

Commit

Permalink
sipf-std-client v0.4.0対応
Browse files Browse the repository at this point in the history
- v0.4.0以降の場合認証モード設定をスキップ
- ファイル送信対応
  - ButtonA: TX, ButtonB: RX, ButtonC: FILE PUTに再割当て
  - XMODEMライブラリ追加
  - $$FPUTコマンド送信を追加
  • Loading branch information
f-okuhara committed Feb 28, 2022
1 parent 8a935c7 commit ff6825b
Show file tree
Hide file tree
Showing 6 changed files with 605 additions and 32 deletions.
73 changes: 43 additions & 30 deletions sipf-std-m5stack/sipf-std-m5stack.ino
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
* SIPF接続情報
*/
static uint8_t buff[256];
static uint32_t cnt_btn1, cnt_btn2;
static uint32_t cnt_btn1;

static int resetSipfModule()
{
Expand Down Expand Up @@ -100,11 +100,16 @@ static void drawButton(uint8_t button, uint32_t value)
M5.Lcd.setTextColor(TFT_BLACK, 0xfaae);
M5.Lcd.setCursor(40 + (95 * button), 210);

if (button != 2) {
switch (button) {
case 0: // TX
M5.Lcd.printf("%4d", value);
} else {
//受信ボタン
break;
case 1: // RX
M5.Lcd.printf(" RX ");
break;
case 2: // FILE PUT
M5.Lcd.printf("FILE");
break;
}
}

Expand Down Expand Up @@ -185,16 +190,19 @@ void setup() {
return;
}

if (SipfGetFwVersion() != 0) {
uint32_t fw_version;
if (SipfGetFwVersion(&fw_version) != 0) {
M5.Lcd.printf("SipfGetFwVersion(): FAILED\r\n");
}

M5.Lcd.printf("Setting auth mode...");
if (SipfSetAuthMode(0x01) == 0) {
M5.Lcd.printf(" OK\n");
} else {
M5.Lcd.printf(" NG\n");
return;
if (fw_version < 0x000400) {
M5.Lcd.printf("Setting auth mode...");
if (SipfSetAuthMode(0x01) == 0) {
M5.Lcd.printf(" OK\n");
} else {
M5.Lcd.printf(" NG\n");
return;
}
}
#ifdef ENABLE_GNSS
M5.Lcd.printf("Enable GNSS..");
Expand All @@ -208,10 +216,9 @@ void setup() {
drawResultWindow();

cnt_btn1 = 0;
cnt_btn2 = 0;

drawButton(0, cnt_btn1);
drawButton(1, cnt_btn2);
drawButton(1, 0);
drawButton(2, 0);

Serial.println("+++ Ready +++");
Expand Down Expand Up @@ -263,25 +270,10 @@ void loop() {
}
}

/* `TX2'ボタンを押した */
if (M5.BtnB.wasPressed()) {
cnt_btn2++;
drawResultWindow();
M5.Lcd.printf("ButtonB pushed: TX(tag_id=0x02 value=%d)\n", cnt_btn2);
memset(buff, 0, sizeof(buff));
int ret = SipfCmdTx(0x02, OBJ_TYPE_UINT32, (uint8_t*)&cnt_btn2, 4, buff);
if (ret == 0) {
M5.Lcd.printf("OK\nOTID: %s\n", buff);
drawButton(1, cnt_btn2);
} else {
M5.Lcd.printf("NG: %d\n", ret);
}
}

/* `RX'ボタンを押した */
if (M5.BtnC.wasPressed()) {
if (M5.BtnB.wasPressed()) {
drawResultWindow();
M5.Lcd.printf("ButtonC pushed: RX request.\n");
M5.Lcd.printf("ButtonB pushed: RX request.\n");
memset(buff, 0, sizeof(buff));

static SipfObjObject objs[16];
Expand Down Expand Up @@ -376,5 +368,26 @@ void loop() {
}
}

/* `FILE'ボタンを押した */
if (M5.BtnC.wasPressed()) {
drawResultWindow();
M5.Lcd.printf("ButtonC pushed: FILE PUT request.\n");
//テキストファイルを用意
int len = sprintf((char *)buff, "millis(): 0x%08x\r\n", millis());
len += sprintf((char *)&buff[len], "Tx count: %d\r\n", cnt_btn1);
for (int i = 0; i < (sizeof(buff) - len); i++) {
buff[len+i] = (uint8_t)(i % 0x5f) + 0x20;
}

M5.Lcd.printf("\n\n%s\n\n", (char *)buff);

int ret = SipfCmdFput("FPUT_SAMPLE_M5.txt", buff, sizeof(buff));
if (ret == 0) {
M5.Lcd.printf("OK\n");
} else {
M5.Lcd.printf("NG: %d\n", ret);
}
}

M5.update();
}
102 changes: 101 additions & 1 deletion sipf-std-m5stack/sipf_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
*/
#include <Arduino.h>
#include "sipf_client.h"
#include "xmodem.h"
#include <stdio.h>
#include <string.h>

#define FPUT_RETRY_MAX (3)

static char cmd[512];
static uint32_t fw_version;

Expand Down Expand Up @@ -219,7 +222,7 @@ int SipfSetAuthInfo(char *user_name, char *password)
/**
* Fwバージョンを取得
*/
int SipfGetFwVersion(void)
int SipfGetFwVersion(uint32_t *version)
{
uint8_t v = 0;
if (sipfSendR(0xf1, &v) != 0) {
Expand All @@ -239,6 +242,10 @@ int SipfGetFwVersion(void)
}
fw_version |= (uint32_t)v << 8; // RELEASE上位

if (version) {
*version = fw_version;
}

return 0;
}

Expand Down Expand Up @@ -699,3 +706,96 @@ int SipfCmdRx(uint8_t *otid, uint64_t *user_send_datetime_ms, uint64_t *sipf_rec
}
}
}

/**
* $$FPUT送信
*/
static int sipfCmdFputWaitNg(void)
{
int ret;
for (;;) {
ret = SipfUtilReadLine((uint8_t*)cmd, sizeof(cmd), TMOUT_CHAR);
if (ret == -3) {
//タイムアウト
return -3;
}
if (memcmp(cmd, "NG", 2) == 0) {
//OK
break;
}
}
return 0;
}
static uint8_t buf_xmodem_buff[XMODEM_SZ_BLOCK];
int SipfCmdFput(char *file_id, uint8_t *file_body, size_t sz_file)
{
int len, ret;
//UART受信バッファを読み捨てる
SipfClientFlushReadBuff();
// $$FPUTコマンド送信
len = sprintf(cmd, "$$FPUT %s %08X\r\n", file_id, sz_file);
ret = Serial2.write((uint8_t*)cmd, len);

// XMODEM開始
XmodemBegin();
// 送信要求待ち(タイムアウト30秒)
XmodemSendRet xret = XmodemSendWaitRequest(30000);
switch (xret) {
case XMODEM_SEND_RET_OK:
break;
case XMODEM_SEND_RET_CANCELED:
default:
// NG待ち
sipfCmdFputWaitNg();
return xret;
}
// ブロック送信
uint8_t fget_bn = 1;
size_t sz_block;
for (int idx = 0; idx < sz_file; idx += XMODEM_SZ_BLOCK) {
for (int i = 0; i < FPUT_RETRY_MAX; i++) {
if ((sz_file - idx) < XMODEM_SZ_BLOCK) {
sz_block = len % XMODEM_SZ_BLOCK;
} else {
sz_block = XMODEM_SZ_BLOCK;
}
xret = XmodemSendBlock(&fget_bn, &file_body[idx], sz_block, 10000); //タイムアウト10秒
switch (xret) {
case XMODEM_SEND_RET_OK:
goto next_block;
case XMODEM_SEND_RET_CANCELED:
// NG待ち
sipfCmdFputWaitNg();
return xret;
case XMODEM_SEND_RET_RETRY:
// 同じブロックを再送
continue;
case XMODEM_SEND_RET_TIMEOUT:
case XMODEM_SEND_RET_FAILED:
// NG待ち
sipfCmdFputWaitNg();
return xret;
}
break;
}
next_block:
fget_bn++; // ブロック番号を加算
}

// XMODEM転送終了
XmodemSendEnd(500);

// $$FGETコマンドの応答を見る
for (;;) {
ret = SipfUtilReadLine((uint8_t*)cmd, sizeof(cmd), TMOUT_CMD);
if (ret == -3) {
// タイムアウト
return -3;
}
if (memcmp(cmd, "OK", 2) == 0) {
// OK
break;
}
}
return 0;
}
4 changes: 3 additions & 1 deletion sipf-std-m5stack/sipf_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,13 @@ typedef struct {
int SipfSetAuthMode(uint8_t mode);
int SipfSetAuthInfo(char *user_name, char *password);

int SipfGetFwVersion(void);
int SipfGetFwVersion(uint32_t *version);

int SipfCmdTx(uint8_t tag_id, SipfObjTypeId type, uint8_t *value, uint8_t value_len, uint8_t *otid);
int SipfCmdRx(uint8_t *otid, uint64_t *user_send_datetime_ms, uint64_t *sipf_recv_datetime_ms, uint8_t *remain, uint8_t *obj_cnt, SipfObjObject *obj_list, uint8_t obj_list_sz);

int SipfCmdFput(char *file_id, uint8_t *file_body, size_t sz_file);

int SipfUtilReadLine(uint8_t *buff, int buff_len, int timeout_ms);
void SipfClientFlushReadBuff(void);

Expand Down
Loading

0 comments on commit ff6825b

Please sign in to comment.