코드 정리

- 주석 영문으로 변경
- Allman 스타일로 통일
This commit is contained in:
2026-04-16 12:01:51 +09:00
parent c98d9ae14e
commit 2861cb9815
35 changed files with 2406 additions and 2557 deletions

View File

@@ -1,10 +1,9 @@
/*==============================================================================
* cmd_table.c - Command table definition
*
* This file is the only linkage point between parser.c and the cmd_*.c
* handler modules. It pulls in every cmd_*.h to gather handler prototypes,
* builds the CmdEntry array, and injects the table into the parser via
* dr_parser_init() inside cmd_table_init().
* This file is the only linkage point between parser.c and the cmd_*.c handler modules.
* It pulls in every cmd_*.h to gather handler prototypes,
* builds the CmdEntry array, and injects the table into the parser via dr_parser_init() inside cmd_table_init().
*
* Adding a new command:
* 1) Implement the handler in the appropriate cmd_*.c

View File

@@ -19,9 +19,8 @@
* Request: [TAG 4B "msq?"] [val 2B BE] [CRC 2B]
* Response: [TAG 4B "rsq:"] [val 2B BE] [CRC 2B]
*
* Sends BLE response first; the actual power-off is performed from the main
* loop. Powering off immediately would prevent the response from reaching
* the host, so a flag + timer pattern is used instead.
* Sends BLE response first; the actual power-off is performed from the main loop.
* Powering off immediately would prevent the response from reaching the host, so a flag + timer pattern is used instead.
*============================================================================*/
int Cmd_msq(const ParsedCmd *cmd)
{
@@ -44,8 +43,7 @@ int Cmd_msq(const ParsedCmd *cmd)
* Request: [TAG 4B "mss?"] [val 2B BE] [CRC 2B]
* Response: [TAG 4B "rss:"] [val 2B BE] [CRC 2B]
*
* Resets without erasing bond information. Reset status code is persisted
* to FDS so the boot path can identify the cause.
* Resets without erasing bond information. Reset status code is persisted to FDS so the boot path can identify the cause.
*============================================================================*/
int Cmd_mss(const ParsedCmd *cmd)
{
@@ -118,9 +116,18 @@ int Cmd_cmd(const ParsedCmd *cmd)
return 1;
}
if (!dr_get_u16(cmd, 0, &v1)) v1 = 0;
if (!dr_get_u16(cmd, 1, &v2)) v2 = 0;
if (!dr_get_u16(cmd, 2, &v3)) v3 = 0;
if (!dr_get_u16(cmd, 0, &v1))
{
v1 = 0;
}
if (!dr_get_u16(cmd, 1, &v2))
{
v2 = 0;
}
if (!dr_get_u16(cmd, 2, &v3))
{
v3 = 0;
}
pin_number = NRF_GPIO_PIN_MAP(v1, v2);
nrf_gpio_cfg_output(pin_number);
@@ -142,15 +149,15 @@ int Cmd_cmd(const ParsedCmd *cmd)
* mls? -> rls: Set LED state (app -> device)
*
* Request: [TAG 4B "mls?"] [state 2B BE] [CRC 2B]
* state: led_state_t enum value
* 0=OFF, 4=DETACH_WARNING, 5=ALIGN_SEARCHING, 6=ALIGN_COMPLETE
* state: led_state_t enum value: 0=OFF, 4=DETACH_WARNING, 5=ALIGN_SEARCHING, 6=ALIGN_COMPLETE
* Response: [TAG 4B "rls:"] [state 2B] [CRC 2B]
* Error: rls: + 0xFFFF (insufficient data)
* rls: + 0xFFFE (state out of range)
*============================================================================*/
int Cmd_mls(const ParsedCmd *cmd)
{
if (cmd->data_len < 2) {
if (cmd->data_len < 2)
{
dr_ble_return_1("rls:", 0xFFFF);
return 1;
}
@@ -158,7 +165,8 @@ int Cmd_mls(const ParsedCmd *cmd)
uint16_t state;
dr_get_u16(cmd, 0, &state);
if (state > LED_STATE_ERROR) {
if (state > LED_STATE_ERROR)
{
dr_ble_return_1("rls:", 0xFFFE);
return 1;
}

View File

@@ -57,7 +57,8 @@ int Cmd_mwh(const ParsedCmd *cmd)
{
char buf[13];
if (cmd->data_len < 12) {
if (cmd->data_len < 12)
{
dr_ble_return_1("rwh:", 0xFFFF);
return 1;
}
@@ -83,7 +84,8 @@ int Cmd_mws(const ParsedCmd *cmd)
{
char buf[13];
if (cmd->data_len < 12) {
if (cmd->data_len < 12)
{
dr_ble_return_1("rws:", 0xFFFF);
return 1;
}

View File

@@ -22,15 +22,14 @@
*
* Request: [TAG 4B "mpa?"] [CRC 2B]
* Response: [TAG 4B "rpa:"] [1 2B] [CRC 2B]
*
* Must be called before mec? / maa?.
*============================================================================*/
int Cmd_mpa(const ParsedCmd *cmd)
{
(void)cmd;
dr_piezo_power_on();
if (g_plat.tx_bin) {
if (g_plat.tx_bin)
{
single_format_data(ble_bin_buffer, "rpa:", 1);
dr_binary_tx_safe(ble_bin_buffer, 3);
}
@@ -50,7 +49,8 @@ int Cmd_mpb(const ParsedCmd *cmd)
(void)cmd;
dr_piezo_power_off();
if (g_plat.tx_bin) {
if (g_plat.tx_bin)
{
single_format_data(ble_bin_buffer, "rpb:", 1);
dr_binary_tx_safe(ble_bin_buffer, 3);
}
@@ -79,22 +79,36 @@ int Cmd_mpc(const ParsedCmd *cmd)
(void)dr_get_u16(cmd, 1, &freq_option);
(void)dr_get_u16(cmd, 2, &piezo_ch);
if (piezo_ch >= MAA_NUM_CHANNELS) piezo_ch = 0;
if (piezo_ch >= MAA_NUM_CHANNELS)
{
piezo_ch = 0;
}
if (cycles < 3 || cycles > 7) {
if (cycles < 3 || cycles > 7)
{
dr_ble_return_1("rpc:", 2);
return 1;
}
dr_piezo_select_channel((uint8_t)piezo_ch);
switch (freq_option) {
case 0: dr_piezo_burst_sw_18mhz((uint8_t)cycles); break;
case 2: dr_piezo_burst_sw_20mhz((uint8_t)cycles); break;
case 3: dr_piezo_burst_sw_17mhz((uint8_t)cycles); break;
case 4: dr_piezo_burst_sw_22mhz((uint8_t)cycles); break;
case 1:
default: dr_piezo_burst_sw((uint8_t)cycles); break;
switch (freq_option)
{
case 0:
dr_piezo_burst_sw_18mhz((uint8_t)cycles);
break;
case 2:
dr_piezo_burst_sw_20mhz((uint8_t)cycles);
break;
case 3:
dr_piezo_burst_sw_17mhz((uint8_t)cycles);
break;
case 4:
dr_piezo_burst_sw_22mhz((uint8_t)cycles);
break;
default:
dr_piezo_burst_sw((uint8_t)cycles);
break;
}
dr_ble_return_1("rpc:", (uint8_t)cycles);
@@ -121,7 +135,8 @@ int Cmd_mec(const ParsedCmd *cmd)
uint16_t averaging = 1;
uint16_t piezo_ch = 0;
if (!dr_piezo_is_power_on()) {
if (!dr_piezo_is_power_on())
{
dr_piezo_power_on();
}
@@ -132,15 +147,25 @@ int Cmd_mec(const ParsedCmd *cmd)
(void)dr_get_u16(cmd, 4, &averaging);
(void)dr_get_u16(cmd, 5, &piezo_ch);
if (averaging == 0) averaging = 1;
if (averaging > 1000) averaging = 1000;
if (piezo_ch >= MAA_NUM_CHANNELS) piezo_ch = 0;
if (averaging == 0)
{
averaging = 1;
}
if (averaging > 1000)
{
averaging = 1000;
}
if (piezo_ch >= MAA_NUM_CHANNELS)
{
piezo_ch = 0;
}
dr_adc_err_t err = dr_adc_burst_capture_transmit(
(uint8_t)freq_option, delay_us, num_samples, (uint8_t)cycles,
(uint16_t)averaging, (uint8_t)piezo_ch, ble_bin_buffer, 0);
if (err != DR_ADC_OK) {
if (err != DR_ADC_OK)
{
dr_ble_return_2("rer:", 0xEE00 | (uint16_t)err, num_samples);
}
@@ -165,12 +190,14 @@ int Cmd_maa(const ParsedCmd *cmd)
dr_adc_err_t err;
(void)cmd;
if (maa_async_is_busy()) {
if (maa_async_is_busy())
{
dr_ble_return_1("raa:", 0xFFFE);
return 1;
}
if (!dr_piezo_is_power_on()) {
if (!dr_piezo_is_power_on())
{
dr_piezo_power_on();
}
@@ -183,8 +210,12 @@ int Cmd_maa(const ParsedCmd *cmd)
ble_bin_buffer
);
if (err != DR_ADC_OK) {
if (g_plat.log) g_plat.log("[Cmd_maa] start failed err=%d\r\n", err);
if (err != DR_ADC_OK)
{
if (g_plat.log)
{
g_plat.log("[Cmd_maa] start failed err=%d\r\n", err);
}
single_format_data(ble_bin_buffer, "raa:", (uint16_t)(0xFF00 | err));
dr_binary_tx_safe(ble_bin_buffer, 3);
dr_piezo_power_off();
@@ -214,7 +245,8 @@ int Cmd_mbb(const ParsedCmd *cmd)
all_sensors();
if (maa_async_is_busy()) {
if (maa_async_is_busy())
{
dr_ble_return_1("raa:", 0xFFFE);
return 1;
}
@@ -230,8 +262,12 @@ int Cmd_mbb(const ParsedCmd *cmd)
ble_bin_buffer
);
if (err != DR_ADC_OK) {
if (g_plat.log) g_plat.log("[Cmd_mbb] start failed err=%d\r\n", err);
if (err != DR_ADC_OK)
{
if (g_plat.log)
{
g_plat.log("[Cmd_mbb] start failed err=%d\r\n", err);
}
single_format_data(ble_bin_buffer, "raa:", (uint16_t)(0xFF00 | err));
dr_binary_tx_safe(ble_bin_buffer, 3);
dr_piezo_power_off();
@@ -245,8 +281,7 @@ int Cmd_mbb(const ParsedCmd *cmd)
* mcf? -> rcf: Read piezo parameters from FDS
*
* Request: [TAG 4B "mcf?"] [CRC 2B]
* Response: [TAG 4B "rcf:"] [freq 2B] [cycles 2B] [avg 2B]
* [delay_us 2B] [num_samples 2B] [CRC 2B]
* Response: [TAG 4B "rcf:"] [freq 2B] [cycles 2B] [avg 2B] [delay_us 2B] [num_samples 2B] [CRC 2B]
*============================================================================*/
int Cmd_mcf(const ParsedCmd *cmd)
{
@@ -266,15 +301,18 @@ int Cmd_mcf(const ParsedCmd *cmd)
/*==============================================================================
* mcs? -> rcs: Write piezo parameters to FDS
*
* Request: [TAG 4B "mcs?"] [freq 2B] [cycles 2B] [avg 2B]
* [delay_us 2B] [num_samples 2B] [CRC 2B]
* Request: [TAG 4B "mcs?"] [freq 2B] [cycles 2B] [avg 2B] [delay_us 2B] [num_samples 2B] [CRC 2B]
* Response: [TAG 4B "rcs:"] [stored 5 values] [CRC 2B]
* Error: rcs: + 0xFFFF (insufficient data)
*============================================================================*/
int Cmd_mcs(const ParsedCmd *cmd)
{
if (cmd->data_len < 10) {
if (g_plat.log) g_plat.log("[Cmd_mcs] missing params (data_len=%u)\r\n", cmd->data_len);
if (cmd->data_len < 10)
{
if (g_plat.log)
{
g_plat.log("[Cmd_mcs] missing params (data_len=%u)\r\n", cmd->data_len);
}
dr_ble_return_1("rcs:", 0xFFFF);
return 1;
}

View File

@@ -69,9 +69,12 @@ int Cmd_msi(const ParsedCmd *cmd)
motion_raw_data_enabled = true;
ble_got_new_data = false;
if (cmd->data_len > 0 && (char)cmd->data[0] == 'c') {
if (cmd->data_len > 0 && (char)cmd->data[0] == 'c')
{
motion_data_once = false;
} else {
}
else
{
motion_data_once = true;
}
@@ -99,7 +102,8 @@ void all_sensors(void)
/* 1. Battery voltage -> info_batt */
battery_saadc_done = false;
battery_level_meas();
for (timeout_cnt = 0; !battery_saadc_done && timeout_cnt < 100; timeout_cnt++) {
for (timeout_cnt = 0; !battery_saadc_done && timeout_cnt < 100; timeout_cnt++)
{
dr_sd_delay_ms(1);
}
@@ -108,13 +112,15 @@ void all_sensors(void)
imu_read_direct();
/* 3. Temperature -> info_temp (TMP235 needs Piezo TX/RX power) */
if (!dr_piezo_is_power_on()) {
if (!dr_piezo_is_power_on())
{
dr_piezo_power_on();
}
tmp235_saadc_done = false;
tmp235_voltage_level_meas();
for (timeout_cnt = 0; !tmp235_saadc_done && timeout_cnt < 100; timeout_cnt++) {
for (timeout_cnt = 0; !tmp235_saadc_done && timeout_cnt < 100; timeout_cnt++)
{
dr_sd_delay_ms(1);
}
@@ -126,7 +132,8 @@ void all_sensors(void)
buf[4] = (uint8_t)(info_batt >> 8);
buf[5] = (uint8_t)(info_batt & 0xFF);
for (int i = 0; i < 6; i++) {
for (int i = 0; i < 6; i++)
{
buf[6 + i * 2] = (uint8_t)(info_imu[i] >> 8);
buf[6 + i * 2 + 1] = (uint8_t)(info_imu[i] & 0xFF);
}

View File

@@ -11,9 +11,8 @@ int Cmd_mso(const ParsedCmd *cmd); /* mso? -> rso: TMP235 temperature reading
int Cmd_msp(const ParsedCmd *cmd); /* msp? -> rsp: IMU 6-axis single read */
int Cmd_msi(const ParsedCmd *cmd); /* msi? -> rsi: IMU streaming start */
/* Helper for the mbb? handler: sequentially measures battery / IMU /
* temperature, then emits a single rbb: response. Called from
* Cmd_mbb() in cmd_piezo.c. */
/* Helper for the mbb? handler: sequentially measures battery / IMU / temperature, then emits a single rbb: response.
* Called from Cmd_mbb() in cmd_piezo.c. */
void all_sensors(void);
#endif /* CMD_SENSOR_H */

View File

@@ -74,11 +74,11 @@ static bool dr_tag_eq(const char *tag, const char *key4)
bool dr_get_u16(const ParsedCmd *cmd, uint8_t word_index, uint16_t *out)
{
uint8_t pos = (uint8_t)(word_index * 2);
if (cmd->data_len < (uint8_t)(pos + 2)) {
if (cmd->data_len < (uint8_t)(pos + 2))
{
return false;
}
*out = (uint16_t)((uint16_t)cmd->data[pos] << 8)
| (uint16_t)cmd->data[pos + 1];
*out = (uint16_t)((uint16_t)cmd->data[pos] << 8) | (uint16_t)cmd->data[pos + 1];
return true;
}
@@ -87,17 +87,20 @@ void dr_get_ascii(const ParsedCmd *cmd, uint8_t offset, char *out, uint8_t max_l
uint8_t i;
uint8_t remain;
if (offset >= cmd->data_len) {
if (offset >= cmd->data_len)
{
out[0] = '\0';
return;
}
remain = (uint8_t)(cmd->data_len - offset);
if (remain > max_len) {
if (remain > max_len)
{
remain = max_len;
}
for (i = 0; i < remain; i++) {
for (i = 0; i < remain; i++)
{
out[i] = (char)cmd->data[offset + i];
}
out[remain] = '\0';
@@ -113,7 +116,8 @@ uint16_t dr_crc16_compute(const uint8_t *p_data, uint32_t size, const uint16_t *
uint32_t i;
uint16_t crc = (p_crc == NULL) ? 0xFFFF : *p_crc;
for (i = 0; i < size; i++) {
for (i = 0; i < size; i++)
{
crc = (uint8_t)(crc >> 8) | (crc << 8);
crc ^= p_data[i];
crc ^= (uint8_t)(crc & 0xFF) >> 4;
@@ -134,7 +138,8 @@ static bool dr_crc16_check_packet(const uint8_t *packet, uint32_t packet_len)
uint16_t expected_crc;
uint32_t data_len;
if (packet_len < 2) {
if (packet_len < 2)
{
return false;
}
@@ -154,7 +159,8 @@ static bool dr_crc16_check_packet(const uint8_t *packet, uint32_t packet_len)
*============================================================================*/
static void dr_send_error(const char *err_tag, const char *cmd_tag)
{
if (g_plat.tx_bin) {
if (g_plat.tx_bin)
{
uint8_t err_buf[8];
memcpy(&err_buf[0], err_tag, 4);
memcpy(&err_buf[4], cmd_tag, 4);
@@ -173,9 +179,11 @@ static bool dr_parse_cmd(const uint8_t *buffer, uint8_t length, ParsedCmd *out)
uint8_t data_len;
/* Less than 4 bytes -> TAG cannot be identified */
if (length < 4) {
if (length < 4)
{
dr_send_error("rxs:", "????");
if (g_plat.log && g_log_enable) {
if (g_plat.log && g_log_enable)
{
g_plat.log("[parser] too short (%u bytes) -> rxs:\r\n", length);
}
return false;
@@ -185,18 +193,23 @@ static bool dr_parse_cmd(const uint8_t *buffer, uint8_t length, ParsedCmd *out)
dr_copy_tag(buffer, out->tag);
/* CRC verification */
if (g_plat.crc_check) {
if (length < 7) {
if (g_plat.crc_check)
{
if (length < 7)
{
dr_send_error("rxs:", out->tag);
if (g_plat.log && g_log_enable) {
if (g_plat.log && g_log_enable)
{
g_plat.log("[parser] CRC enabled but too short (%u) -> rxs:\r\n", length);
}
return false;
}
if (!dr_crc16_check_packet(buffer, length)) {
if (!dr_crc16_check_packet(buffer, length))
{
dr_send_error("rxc:", out->tag);
if (g_plat.log && g_log_enable) {
if (g_plat.log && g_log_enable)
{
g_plat.log("[parser] CRC mismatch '%s' -> rxc:\r\n", out->tag);
}
return false;
@@ -204,15 +217,18 @@ static bool dr_parse_cmd(const uint8_t *buffer, uint8_t length, ParsedCmd *out)
data_len = (uint8_t)(length - 4 - 2); /* strip TAG and CRC */
}
else {
else
{
data_len = (uint8_t)(length - 4);
}
if (data_len > DR_MAX_DATA) {
if (data_len > DR_MAX_DATA)
{
data_len = DR_MAX_DATA;
}
if (data_len > 0) {
if (data_len > 0)
{
memcpy(out->data, buffer + 4, data_len);
}
out->data_len = data_len;
@@ -232,33 +248,43 @@ static int dr_cmd_dispatch(const ParsedCmd *cmd)
uint16_t i;
char tag_lower[5];
if (m_cmd_table == NULL) {
if (m_cmd_table == NULL)
{
dr_send_error("rxn:", cmd->tag);
if (g_plat.log) g_plat.log("[parser] table not initialized -> rxn:\n");
if (g_plat.log)
{
g_plat.log("[parser] table not initialized -> rxn:\n");
}
return 0;
}
/* Case-insensitive matching */
for (i = 0; i < 4 && cmd->tag[i]; i++) {
tag_lower[i] = (cmd->tag[i] >= 'A' && cmd->tag[i] <= 'Z')
? (cmd->tag[i] + 32) : cmd->tag[i];
for (i = 0; i < 4 && cmd->tag[i]; i++)
{
tag_lower[i] = (cmd->tag[i] >= 'A' && cmd->tag[i] <= 'Z') ? (cmd->tag[i] + 32) : cmd->tag[i];
}
tag_lower[i] = '\0';
for (i = 0; i < m_cmd_count; i++) {
if (dr_tag_eq(tag_lower, m_cmd_table[i].tag)) {
for (i = 0; i < m_cmd_count; i++)
{
if (dr_tag_eq(tag_lower, m_cmd_table[i].tag))
{
if (!m_cmd_table[i].enabled) {
if (!m_cmd_table[i].enabled)
{
dr_send_error("rxd:", cmd->tag);
if (g_plat.log && g_log_enable) {
if (g_plat.log && g_log_enable)
{
g_plat.log("Command '%s' disabled -> rxd:\n", cmd->tag);
}
return 0;
}
if (m_cmd_table[i].handler == NULL) {
if (m_cmd_table[i].handler == NULL)
{
dr_send_error("rxn:", cmd->tag);
if (g_plat.log) {
if (g_plat.log)
{
g_plat.log("[parser] NULL handler for '%s' -> rxn:\n", cmd->tag);
}
return 0;
@@ -270,7 +296,8 @@ static int dr_cmd_dispatch(const ParsedCmd *cmd)
/* TAG not found in table */
dr_send_error("rxx:", cmd->tag);
if (g_plat.log && g_log_enable) {
if (g_plat.log && g_log_enable)
{
g_plat.log("Unknown TAG '%s' -> rxx:\n", cmd->tag);
}
return 0;
@@ -299,7 +326,8 @@ int dr_cmd_parser(const uint8_t *buf, uint8_t len)
{
ParsedCmd cmd;
if (!dr_parse_cmd(buf, len, &cmd)) {
if (!dr_parse_cmd(buf, len, &cmd))
{
if (g_plat.log) g_plat.log("[PARSER] PARSE FAIL\r\n");
return -1;
}