Skip to content

Commit

Permalink
Event template implementation added (#101)
Browse files Browse the repository at this point in the history
  • Loading branch information
HaseenaSainul committed Aug 8, 2023
1 parent 8e716e5 commit 8d9ac14
Show file tree
Hide file tree
Showing 10 changed files with 260 additions and 83 deletions.
92 changes: 48 additions & 44 deletions languages/c/Types.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@

import deepmerge from 'deepmerge'
import { getPath } from '../../src/shared/json-schema.mjs'
import { getTypeName, getModuleName, description, getObjectHandleManagement, getNativeType, getPropertyAccessors, capitalize, isOptional, generateEnum, getMapAccessors, getArrayAccessors, getArrayElementSchema, getPropertyGetterSignature, getPropertyEventCallbackSignature, getPropertyEventRegisterSignature, getPropertyEventUnregisterSignature, getPropertySetterSignature } from './src/types/NativeHelpers.mjs'
import { getArrayAccessorsImpl, getMapAccessorsImpl, getObjectHandleManagementImpl, getParameterInstantiation, getPropertyAccessorsImpl, getResultInstantiation } from './src/types/ImplHelpers.mjs'
import { getTypeName, getModuleName, description, getObjectHandleManagement, getNativeType, getPropertyAccessors, capitalize, isOptional, generateEnum, getMapAccessors, getArrayAccessors, getArrayElementSchema, getPropertyGetterSignature, getPropertyEventCallbackSignature, getPropertyEventRegisterSignature, getPropertyEventUnregisterSignature, getPropertySetterSignature, getFireboltStringType } from './src/types/NativeHelpers.mjs'
import { getArrayAccessorsImpl, getMapAccessorsImpl, getObjectHandleManagementImpl, getParameterInstantiation, getPropertyAccessorsImpl, getResultInstantiation, getCallbackParametersInstantiation, getCallbackResultInstantiation, getCallbackResponseInstantiation } from './src/types/ImplHelpers.mjs'
import { getJsonContainerDefinition, getJsonDataStructName } from './src/types/JSONHelpers.mjs'

const getSdkNameSpace = () => 'FireboltSDK'
Expand Down Expand Up @@ -170,44 +170,50 @@ const hasTag = (method, tag) => {
return method.tags && method.tags.filter(t => t.name === tag).length > 0
}

function getMethodSignature(method, module, { destination, isInterface = false }) {

let signature = ''
function getParamList(schema, module) {
let paramList = []
if(hasTag(method, 'property') || hasTag(method, 'property:readonly') || hasTag(method, 'property:immutable')) {

method.params.map(param => {
if (schema.params.length > 0) {
schema.params.map(p => {
/*
paramList = [{name='', nativeType='', jsonType='', required=boolean}]
param = {name='', nativeType='', jsonType='', required=boolean}
*/
paramList['nativeType'] = getSchemaType(param.schema, module, { title: true, name: param.name })
paramList['jsonType'] = getJsonType(param.schema, module, {name: param.name})
paramList['name'] = param.name
paramList['required'] = param.required

let param = {}
param['nativeType'] = getSchemaType(p.schema, module, { title: true, name: p.name })
param['jsonType'] = getJsonType(p.schema, module, {name: p.name})
param['name'] = p.name
param['required'] = p.required
paramList.push(param)
})

}
return paramList
}

function getMethodSignature(method, module, { destination, isInterface = false }) {

let signature = ''
if (hasTag(method, 'property') || hasTag(method, 'property:readonly') || hasTag(method, 'property:immutable')) {
let paramList = getParamList(method, module)
let resultType = method.result && getSchemaType(method.result.schema, module, { title: true, name: method.result.name, resultSchema: true}) || ''

signature = getPropertyGetterSignature(method, module, resultType, paramList) + ';\n\n'

if(hasTag(method, 'property') || hasTag(method, 'property:readonly')) {
signature += getPropertyEventCallbackSignature(method, module, resultType, paramList) + ';\n\n'
signature += getPropertyEventRegisterSignature(method, module, paramList) + ';\n\n'
signature += getPropertyEventUnregisterSignature(method, module) + ';\n\n'
}

if(hasTag(method, 'property')) {
if (hasTag(method, 'property')) {
signature += getPropertySetterSignature(method, module, resultType, paramList) + ';\n\n'
}
}
return signature
}

function getMethodSignatureParams(method, module, { destination, callback= false } = {}) {

function getMethodSignatureParams(method, module, { destination }) {

return method.params.map(param => getSchemaType(param.schema, module, { name: param.name, title: true, destination }) + (!param.required ? '* ' : ' ') + param.name ).join(', ')
return method.params.map(param => {
let type = getSchemaType(param.schema, module, { name: param.name, title: true, destination })
if ((callback === true) && (type === 'char*')) {
type = getFireboltStringType()
}
return type + (!param.required ? '* ' : ' ') + param.name
}).join(', ')
}

const safeName = prop => prop.match(/[.+]/) ? '"' + prop + '"' : prop
Expand Down Expand Up @@ -723,34 +729,32 @@ const enumReducer = (acc, val, i, arr) => {

function getSchemaInstantiation(schema, module, name, { instantiationType = '' } = {}) {

if(instantiationType === 'params') {
if (schema.params.length > 0) {
let paramList = []
schema.params.map(param => {
/*
paramList = [{name='', nativeType='', jsonType='', required=boolean}]
*/
const parameter = {}
parameter['nativeType'] = getSchemaType(param.schema, module, { title: true, name: param.name })
parameter['jsonType'] = getJsonType(param.schema, module, {name: param.name})
parameter['name'] = param.name
parameter['required'] = param.required
paramList.push(parameter)

})
return getParameterInstantiation(paramList)
}
} else if(instantiationType === 'result') {
if (instantiationType === 'params') {
return getParameterInstantiation(getParamList(schema, module))
}
else if (instantiationType === 'result') {
let resultType = getSchemaType(schema, module, { title: true, name: name, resultSchema: true}) || ''
let resultJsonType = getJsonType(schema, module, {name: name}) || ''

return getResultInstantiation(name, resultType, resultJsonType)
}
else if (instantiationType === 'callback.params') {
let resultJsonType = getJsonType(schema.result.schema, module, {name: schema.result.name}) || ''
return getCallbackParametersInstantiation(getParamList(schema, module), resultJsonType)
}
else if (instantiationType === 'callback.result') {
let resultType = getSchemaType(schema.result.schema, module, { title: true, name: schema.result.name, resultSchema: true}) || ''
let resultJsonType = getJsonType(schema.result.schema, module, {name: schema.result.name}) || ''
return getCallbackResultInstantiation(resultType, resultJsonType)
}
else if (instantiationType === 'callback.response') {
let resultType = getSchemaType(schema.result.schema, module, { title: true, name: schema.result.name, resultSchema: true}) || ''
let resultJsonType = getJsonType(schema.result.schema, module, {name: schema.result.name}) || ''
return getCallbackResponseInstantiation(getParamList(schema, module), resultType, resultJsonType)
}

return ''
}


export default {
getMethodSignature,
getMethodSignatureParams,
Expand Down
132 changes: 130 additions & 2 deletions languages/c/src/types/ImplHelpers.mjs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { capitalize } from "./NativeHelpers.mjs"
import { capitalize, getFireboltStringType } from "./NativeHelpers.mjs"

const Indent = '\t'

const getSdkNameSpace = () => 'FireboltSDK'
const wpeJsonNameSpace = () => 'WPEFramework::Core::JSON'
const getFireboltStringType = () => 'FireboltTypes_StringHandle'

const getObjectHandleManagementImpl = (varName, jsonDataName) => {

Expand Down Expand Up @@ -320,12 +319,138 @@ function getParameterInstantiation(paramList, container = '') {
impl += ` jsonParameters.Set(_T("${param.name}"), ${capitalize(param.name)});\n`
impl += ` }`
}
impl += '\n'
}
})

return impl
}

const isNativeType = (type) => (type === 'float' || type === 'char*' || type === 'int32_t' || type === 'bool')

function getCallbackParametersInstantiation(paramList, container = '') {

let impl = ''

if (paramList.length > 0) {
paramList.forEach(param => {
if (param.required !== undefined) {
if (param.nativeType !== 'char*') {
impl += ` ${param.nativeType} ${param.name};\n`
if (param.required === false) {
impl += ` ${param.nativeType}* ${param.name}Ptr = nullptr;\n`
}
}
else {
impl += ` ${getFireboltStringType()} ${param.name};\n`
}
}
})
impl += `\n WPEFramework::Core::ProxyType<${container}>* jsonResponse;\n`
impl += ` WPEFramework::Core::ProxyType<WPEFramework::Core::JSON::VariantContainer>& var = *(static_cast<WPEFramework::Core::ProxyType<WPEFramework::Core::JSON::VariantContainer>*>(response));
ASSERT(var.IsValid() == true);
if (var.IsValid() == true) {
WPEFramework::Core::JSON::VariantContainer::Iterator elements = var->Variants();
while (elements.Next()) {
if (strcmp(elements.Label(), "value") == 0) {
jsonResponse = new WPEFramework::Core::ProxyType<${container}>();
string objectStr;
elements.Current().Object().ToString(objectStr);
(*jsonResponse)->FromString(objectStr);
} else if (strcmp(elements.Label(), "context") == 0) {
WPEFramework::Core::JSON::VariantContainer::Iterator params = elements.Current().Object().Variants();
while (params.Next()) {\n`
let contextParams = ''

paramList.forEach(param => {
if (param.required !== undefined) {
if (isNativeType(param.nativeType) === true) {
if (contextParams.length > 0) {
contextParams += ` else if (strcmp(elements.Label(), "${param.name}") == 0) {\n`
}
else {
contextParams += ` if (strcmp(elements.Label(), "${param.name}") == 0) {\n`
}
if (param.nativeType === 'char*') {
contextParams += ` ${getSdkNameSpace()}::JSON::String* ${param.name}Value = new ${getSdkNameSpace()}::JSON::String();
*${param.name}Value = elements.Current().Value().c_str();
${param.name} = ${param.name}Value;\n`
}
else if (param.nativeType === 'bool') {
contextParams += ` ${param.name} = elements.Current().Boolean();\n`
}
else if ((param.nativeType === 'float') || (param.nativeType === 'int32_t')) {
contextParams += ` ${param.name} = elements.Current().Number();\n`
}
if ((param.nativeType !== 'char*') && (param.required === false)) {
contextParams += ` ${param.name}Ptr = &${param.name};\n`
}
contextParams += ` }\n`
}
}
})
impl += contextParams
impl += ` }
} else {
ASSERT(false);
}
}
}\n`
} else {

impl +=` WPEFramework::Core::ProxyType<${container}>* jsonResponse = static_cast<WPEFramework::Core::ProxyType<${container}>*>(response);\n`
}

return impl
}

function getCallbackResultInstantiation(nativeType, container = '') {
let impl = ''
if (nativeType === 'char*' || nativeType === 'FireboltTypes_StringHandle') {
impl +=`
${container}* jsonStrResponse = new ${container}();
*jsonStrResponse = *(*jsonResponse);
jsonResponse->Release();` + '\n'
}
return impl
}

function getCallbackResponseInstantiation(paramList, nativeType, container = '') {
let impl = ''

if (paramList.length > 0) {
paramList.forEach(param => {
if (param.required !== undefined) {
if (param.nativeType === 'char*') {
impl += `static_cast<${getFireboltStringType()}>(${param.name}), `
}
else if (param.required === true) {
impl += `${param.name}, `
}
else if (param.required === false) {
impl += `${param.name}Ptr, `
}
}
})
}

if (nativeType === 'char*' || nativeType === 'FireboltTypes_StringHandle') {
impl += `static_cast<${nativeType}>(jsonStrResponse)`
}
else if (nativeType.includes('Handle')) {
impl += `static_cast<${nativeType}>(jsonResponse)`
}
else {
impl += `static_cast<${nativeType}>((*jsonResponse)->Value())`
}

return impl
}

function getResultInstantiation (name, nativeType, container, indentLevel = 3) {

let impl = ''
Expand Down Expand Up @@ -353,5 +478,8 @@ export {
getObjectHandleManagementImpl,
getPropertyAccessorsImpl,
getParameterInstantiation,
getCallbackParametersInstantiation,
getCallbackResultInstantiation,
getCallbackResponseInstantiation,
getResultInstantiation
}
13 changes: 8 additions & 5 deletions languages/c/src/types/NativeHelpers.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ const getNativeType = (json, stringAsHandle = false) => {
let jsonType = json.const ? typeof json.const : json.type
if (jsonType === 'string') {
type = 'char*'
if(stringAsHandle) {
if (stringAsHandle) {
type = getFireboltStringType()
}
}
Expand All @@ -140,7 +140,6 @@ const getNativeType = (json, stringAsHandle = false) => {
return type
}


const getObjectHandleManagement = varName => {

let result = `typedef void* ${varName}Handle;
Expand Down Expand Up @@ -229,13 +228,16 @@ function getPropertyGetterSignature(property, module, propType, paramList = [])

let contextParams = ''
contextParams = getContextParams(paramList)
return `uint32_t ${capitalize(getModuleName(module))}_Get${capitalize(property.name)}( ${contextParams}${contextParams.length > 0 ? ', ':''}${propType}* ${property.result.name || property.name})`
return `uint32_t ${capitalize(getModuleName(module))}_Get${capitalize(property.name)}( ${contextParams}${contextParams.length > 0 ? ', ':''}${propType}* ${property.result.name || property.name} )`
}

function getPropertySetterSignature(property, module, propType, paramList = []) {
let contextParams = ''
contextParams = getContextParams(paramList)
return `uint32_t ${capitalize(getModuleName(module))}_Set${capitalize(property.name)}( ${contextParams}${contextParams.length > 0 ? ', ':''}${propType} ${property.result.name || property.name})`
if (propType === getFireboltStringType()) {
propType = 'char*'
}
return `uint32_t ${capitalize(getModuleName(module))}_Set${capitalize(property.name)}( ${contextParams}${contextParams.length > 0 ? ', ':''}${propType} ${property.result.name || property.name} )`
}

function getPropertyEventCallbackSignature(property, module, propType, paramList = []) {
Expand Down Expand Up @@ -288,5 +290,6 @@ export {
getPropertyAccessors,
isOptional,
generateEnum,
getArrayElementSchema
getArrayElementSchema,
getFireboltStringType
}
4 changes: 4 additions & 0 deletions languages/c/templates/declarations/event.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/* ${method.name} - ${method.description} */
typedef void (*${info.Title}${method.Name}Callback)( const void* userData, ${event.signature.callback.params}${if.event.params}, ${end.if.event.params}${event.result.type} );
uint32_t ${info.Title}_Register_${method.Name}( ${event.signature.params}${if.event.params}, ${end.if.event.params}${info.Title}${method.Name}Callback userCB, const void* userData );
uint32_t ${info.Title}_Unregister_${method.Name}( ${info.Title}${method.Name}Callback userCB);
2 changes: 1 addition & 1 deletion languages/c/templates/methods/default.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ uint32_t ${info.title}_${method.Name}(${method.params.list}${if.params}, ${end.i
}

return status;
}
}
26 changes: 26 additions & 0 deletions languages/c/templates/methods/event.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/* ${method.name} - ${method.description} */
static void ${info.Title}${method.Name}InnerCallback( void* userCB, const void* userData, void* response )
{
${event.callback.params.serialization}
ASSERT(jsonResponse->IsValid() == true);
if (jsonResponse->IsValid() == true) {
${event.callback.result.instantiation}
${info.Title}${method.Name}Callback callback = reinterpret_cast<${info.Title}${method.Name}Callback>(userCB);
callback(userData, ${event.callback.response.instantiation});
}
}
uint32_t ${info.Title}_Register_${method.Name}( ${event.signature.params}${if.event.params}, ${end.if.event.params}${info.Title}${method.Name}Callback userCB, const void* userData )
{
const string eventName = _T("${info.title}.${method.name}");
uint32_t status = FireboltSDKErrorNone;

if (userCB != nullptr) {
${event.params.serialization}
status = FireboltSDK::Event::Instance().Subscribe<${event.result.json.type}>(eventName, jsonParameters, ${info.Title}${method.Name}InnerCallback, reinterpret_cast<void*>(userCB), userData);
}
return status;
}
uint32_t ${info.Title}_Unregister_${method.Name}( ${info.Title}${method.Name}Callback userCB)
{
return FireboltSDK::Event::Instance().Unsubscribe(_T("${info.title}.${method.name}"), reinterpret_cast<void*>(userCB));
}
Loading

0 comments on commit 8d9ac14

Please sign in to comment.