Exemple #1
0
static uint8_t prv_create(uint16_t instanceId,
                          int numData,
                          lwm2m_data_t * dataArray,
                          lwm2m_object_t * objectP)
{
    prv_instance_t * targetP;
    uint8_t result;


    targetP = (prv_instance_t *)lwm2m_malloc(sizeof(prv_instance_t));
    if (NULL == targetP) return COAP_500_INTERNAL_SERVER_ERROR;
    memset(targetP, 0, sizeof(prv_instance_t));

    targetP->shortID = instanceId;
    objectP->instanceList = LWM2M_LIST_ADD(objectP->instanceList, targetP);

    result = prv_write(instanceId, numData, dataArray, objectP);

    if (result != COAP_204_CHANGED)
    {
        (void)prv_delete(instanceId, objectP);
    }
    else
    {
        result = COAP_201_CREATED;
    }

    return result;
}
static uint8_t prv_create(uint16_t objInstId, int numData,
                          lwm2m_data_t * tlvArray, lwm2m_object_t * objectP)
{
    acc_ctrl_oi_t * targetP;
    uint8_t result;

    targetP = (acc_ctrl_oi_t *)lwm2m_malloc(sizeof(acc_ctrl_oi_t));
    if (NULL == targetP) return COAP_500_INTERNAL_SERVER_ERROR;
    memset(targetP, 0, sizeof(acc_ctrl_oi_t));

    targetP->objInstId    = objInstId;
    objectP->instanceList = LWM2M_LIST_ADD(objectP->instanceList, targetP);

    result = prv_write_resources(objInstId, numData, tlvArray, objectP, true);

    if (result != COAP_204_CHANGED)
    {
        (void)prv_delete(objInstId, objectP);
    }
    else
    {
        result = COAP_201_CREATED;
    }
    return result;
}
Exemple #3
0
static uint8_t prv_create(uint16_t instanceId, int numData,
		lwm2m_data_t * dataArray, lwm2m_object_t * objectP) {
	// Get user data.
	luaobject_userdata * userdata = (luaobject_userdata*) objectP->userData;
	lua_State * L = userdata->L;

	// Get table of this object on the stack.
	lua_rawgeti(L, LUA_REGISTRYINDEX, userdata->tableref); // stack: ..., object

	// Get the create function
	lua_getfield(L, -1, "create"); // stack: ..., object, createFunc
	if (!lua_isfunction(L, -1)) {
		lua_pop(L, 1); // clean the stack
		return COAP_500_INTERNAL_SERVER_ERROR ;
	}

	// Create instance in C list
	lwm2m_list_t * instance = malloc(sizeof(lwm2m_list_t));
	if (NULL == instance)
		return COAP_500_INTERNAL_SERVER_ERROR;
	memset(instance, 0, sizeof(lwm2m_list_t));
	instance->id = instanceId;
	objectP->instanceList = LWM2M_LIST_ADD(objectP->instanceList, instance)

	// Push object and instance id on the stack and call the create function
	lua_pushvalue(L, -2);  // stack: ..., object, createFunc, object
	lua_pushinteger(L, instanceId); // stack: ..., object, createFunc, object, instanceId
	lua_call(L, 2, 2); // stack: ..., object, return_code, instance

	// Get return code
	int ret = lua_tointeger(L, -2);
	if (ret == COAP_201_CREATED) {
		// write value
		ret = prv_write(instanceId, numData, dataArray, objectP);
		if (ret == COAP_204_CHANGED) {
			return COAP_201_CREATED ;
		} else {
			prv_delete(instanceId, objectP);
			return ret;
		}
	}
	return ret;
}