Exemplo n.º 1
0
// Pads a hex number string representation at a specified length
static char *hex_str_pad(lua_State* L, const char  *str, int len) {
    if (!check_hex_str(str)) {
        luaL_error(L, "invalid hexadecimal number");     
    }
    
    // Allocate string
    char *tmp = (char *)malloc(len + 1);
    if (!tmp) {
        luaL_error(L, "not enough memory");
    }

    if (strlen(str) < len) {
        // Needs pad
        int i;
        int curr_len = strlen(str);
        int pad_num = len - curr_len;
        char *c = tmp;
        
        // Pad with 0
        for(i=0;i < pad_num;i++) {
            *c++ = '0';
        }

       // Copy rest of string
        for(i = pad_num - 1;i < len; i++) {
            *c++ = *str++;
        }
        
        *c = 0x00;
    } else {
        strcpy(tmp, str);
    }

    return tmp;
}
Exemplo n.º 2
0
fm_s32 ascii_to_hex(fm_s8 *in_ascii, fm_u16 *out_hex)
{
	fm_s32 len = (fm_s32) strlen(in_ascii);
	int i = 0;
	fm_u16 tmp;

	len = (len > 4) ? 4 : len;

	if (check_hex_str(in_ascii, len))
		return -1;

	to_upper_n(in_ascii, len);
	*out_hex = 0;

	for (i = 0; i < len; i++) {
		if (in_ascii[len - i - 1] < 'A') {
			tmp = in_ascii[len - i - 1];
			*out_hex |= ((tmp - '0') << (4 * i));
		} else {
			tmp = in_ascii[len - i - 1];
			*out_hex |= ((tmp - 'A' + 10) << (4 * i));
		}
	}

	return 0;
}
Exemplo n.º 3
0
static int llora_tx(lua_State* L) {
    luaL_checktype(L, 1, LUA_TBOOLEAN);
    int cnf = lua_toboolean( L, 1 );
    int port = luaL_checkinteger(L, 2);
    const char *data = luaL_checkstring(L, 3);
    
    if ((port < 1) || (port > 223)) {
        return luaL_error(L, "%d:invalid port number", LORA_INVALID_ARGUMENT);   
    }

    if (!check_hex_str(data)) {
        luaL_error(L, "%d:invalid data", LORA_INVALID_ARGUMENT);     
    }    
    
	uart0_swap();
    int resp = lora_tx(cnf, port, data);
    if (resp != LORA_OK) {
        lora_error(L, resp);
    }
	uart0_default();
    
    return 0;    
}