1 //******************************************************************
3 // Copyright 2015 Intel Mobile Communications GmbH All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
11 // http://www.apache.org/licenses/LICENSE-2.0
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
24 * This file contains APIs for Plugin Interface module to be implemented.
27 #include "plugininterface.h"
28 #include "plugintranslatortypes.h"
29 #include "pluginlist.h"
30 #include "zigbee_wrapper.h"
31 #include "oic_string.h"
32 #include "oic_malloc.h"
34 #include "ocpayload.h"
40 #define TAG PCF("pluginInterface")
43 * Entity handler callback that fills the resPayload of the entityHandlerRequest.
45 OCEntityHandlerResult PluginInterfaceEntityHandler(OCEntityHandlerFlag flag,
46 OCEntityHandlerRequest * entityHandlerRequest,
49 if (!entityHandlerRequest)
51 OC_LOG (ERROR, TAG, "Invalid request pointer");
55 OCEntityHandlerResult ehResult = OC_EH_ERROR;
56 OCStackResult result = OC_STACK_ERROR;
57 PIPluginBase * plugin = (PIPluginBase *) callbackParam;
59 OCEntityHandlerResponse * response =
60 (OCEntityHandlerResponse *) OICCalloc(1, sizeof(*response));
67 OCRepPayload* payload = (OCRepPayload *) entityHandlerRequest->payload;
69 if (flag & OC_REQUEST_FLAG)
71 if (plugin->processEHRequest)
73 ehResult = plugin->processEHRequest(plugin, entityHandlerRequest, &payload);
77 // If the result isn't an error or forbidden, send response
78 if (!((ehResult == OC_EH_ERROR) || (ehResult == OC_EH_FORBIDDEN)))
80 // Format the response. Note this requires some info about the request
81 response->requestHandle = entityHandlerRequest->requestHandle;
82 response->resourceHandle = entityHandlerRequest->resource;
83 response->ehResult = ehResult;
84 response->payload = (OCPayload*) payload;
85 // Indicate that response is NOT in a persistent buffer
86 response->persistentBufferFlag = 0;
88 result = OCDoResponse(response);
89 if (result != OC_STACK_OK)
91 OC_LOG_V(ERROR, TAG, "Error sending response %u", result);
92 ehResult = OC_EH_ERROR;
97 OC_LOG_V(ERROR, TAG, "Error handling request %u", ehResult);
100 OCPayloadDestroy(response->payload);
105 void piNewResourceCB(PIPluginBase * p_plugin, PIResourceBase * r_newResource)
107 if (!p_plugin || !r_newResource)
112 r_newResource->piResource.resourceProperties = OC_DISCOVERABLE;
113 OCStackResult result = OCCreateResource(&r_newResource->piResource.resourceHandle,
114 r_newResource->piResource.resourceTypeName,
115 r_newResource->piResource.resourceInterfaceName,
116 r_newResource->piResource.uri,
117 PluginInterfaceEntityHandler,
119 r_newResource->piResource.resourceProperties);
120 if (result != OC_STACK_OK)
122 OICFree (r_newResource->piResource.uri);
123 OICFree (r_newResource);
126 OC_LOG_V(INFO, TAG, "Created resource of type: %s\n",
127 r_newResource->piResource.resourceTypeName);
129 result = AddResourceToPlugin(p_plugin, r_newResource);
132 OCStackResult PIStartPlugin(const char * comPort, PIPluginType pluginType, PIPlugin ** plugin)
134 if (!plugin || !comPort || strlen(comPort) == 0)
136 return OC_STACK_INVALID_PARAM;
138 OCStackResult result = OC_STACK_ERROR;
139 if (pluginType == PLUGIN_ZIGBEE)
141 result = ZigbeeInit(comPort, (PIPlugin_Zigbee **) plugin, piNewResourceCB);
142 if (result != OC_STACK_OK)
148 return OC_STACK_ERROR;
150 result = AddPlugin((PIPluginBase *) *plugin);
151 if (result == OC_STACK_OK)
153 result = ZigbeeDiscover((PIPlugin_Zigbee *) plugin);
159 OCStackResult PIStopPlugin(PIPlugin * plugin)
163 return OC_STACK_INVALID_PARAM;
166 return DeletePlugin((PIPluginBase *) plugin);
169 OCStackResult PIStopAll()
171 return DeletePluginList();
174 OCStackResult PIProcess(PIPlugin * p_plugin)
176 PIPluginBase * plugin = (PIPluginBase *) p_plugin;
179 return OC_STACK_INVALID_PARAM;
181 OCStackResult result = OC_STACK_ERROR;
182 if (plugin->type == PLUGIN_ZIGBEE)
184 result = ZigbeeProcess((PIPlugin_Zigbee *)plugin);