1 /* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef _NANOHUB_HAL_H_ 18 #define _NANOHUB_HAL_H_ 19 20 #include <pthread.h> 21 22 #include <hardware/context_hub.h> 23 #include <utils/Mutex.h> 24 25 #define NANOAPP_VENDOR_GOOGLE NANOAPP_VENDOR("Googl") 26 27 //as per protocol 28 #define MAX_RX_PACKET 128 29 #define APP_FROM_HOST_EVENT_ID 0x000000F8 30 31 namespace android { 32 33 namespace nanohub { 34 35 void dumpBuffer(const char *pfx, const hub_app_name_t &appId, uint32_t evtId, const void *data, size_t len, int status = 0); 36 37 struct nano_message_hdr { 38 uint32_t event_id; 39 hub_app_name_t app_name; 40 uint8_t len; 41 } __attribute__((packed)); 42 43 struct nano_message { 44 nano_message_hdr hdr; 45 uint8_t data[MAX_RX_PACKET]; 46 } __attribute__((packed)); 47 48 class NanoHub { 49 Mutex mLock; 50 context_hub_callback *mMsgCbkFunc; 51 int mThreadClosingPipe[2]; 52 int mFd; // [0] is read end 53 void * mMsgCbkData; 54 pthread_t mWorkerThread; 55 56 NanoHub() { 57 reset(); 58 } 59 60 void reset() { 61 mThreadClosingPipe[0] = -1; 62 mThreadClosingPipe[1] = -1; 63 mFd = -1; 64 mMsgCbkData = nullptr; 65 mMsgCbkFunc = nullptr; 66 mWorkerThread = 0; 67 } 68 69 static void* run(void *); 70 void* doRun(); 71 72 int openHub(); 73 int closeHub(); 74 75 static NanoHub *hubInstance() { 76 static NanoHub theHub; 77 return &theHub; 78 } 79 80 int doSubscribeMessages(uint32_t hub_id, context_hub_callback *cbk, void *cookie); 81 int doSendToNanohub(uint32_t hub_id, const hub_message_t *msg); 82 int doSendToDevice(const hub_app_name_t *name, const void *data, uint32_t len); 83 void doSendToApp(const hub_app_name_t *name, uint32_t typ, const void *data, uint32_t len); 84 85 static constexpr unsigned int FL_MESSAGE_TRACING = 1; 86 87 unsigned int mFlags = 0; 88 89 public: 90 91 // debugging interface 92 93 static bool messageTracingEnabled() { 94 return hubInstance()->mFlags & FL_MESSAGE_TRACING; 95 } 96 static unsigned int getDebugFlags() { 97 return hubInstance()->mFlags; 98 } 99 static void setDebugFlags(unsigned int flags) { 100 hubInstance()->mFlags = flags; 101 } 102 103 // messaging interface 104 105 // define callback to invoke for APP messages 106 static int subscribeMessages(uint32_t hub_id, context_hub_callback *cbk, void *cookie) { 107 return hubInstance()->doSubscribeMessages(hub_id, cbk, cookie); 108 } 109 // all messages from APP go here 110 static int sendToNanohub(uint32_t hub_id, const hub_message_t *msg) { 111 return hubInstance()->doSendToNanohub(hub_id, msg); 112 } 113 // passes message to kernel driver directly 114 static int sendToDevice(const hub_app_name_t *name, const void *data, uint32_t len) { 115 return hubInstance()->doSendToDevice(name, data, len); 116 } 117 // passes message to APP via callback 118 static void sendToApp(const hub_app_name_t *name, uint32_t typ, const void *data, uint32_t len) { 119 hubInstance()->doSendToApp(name, typ, data, len); 120 } 121 }; 122 123 }; // namespace nanohub 124 125 }; // namespace android 126 127 #endif 128