Rename PortSetDeviceMetadata to PortSetDefaultMetadata
[jack2.git] / android / AndroidShm.cpp
blobff9847c3b8779f420ab8bbe1f1feb996309dbff4
1 #define LOG_TAG "JAMSHMSERVICE"
3 #include <stddef.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <unistd.h>
9 #include <binder/MemoryHeapBase.h>
10 #include <binder/IServiceManager.h>
11 #include <binder/IPCThreadState.h>
12 #include <utils/Log.h>
13 #include <sys/stat.h>
14 #include <sys/socket.h>
15 #include <sys/types.h>
16 #include <sys/un.h>
17 #include <sys/wait.h>
18 #include "BnAndroidShm.h"
19 #include "AndroidShm.h"
22 #include "JackConstants.h"
25 #include <fcntl.h>
26 #include <signal.h>
27 #include <limits.h>
28 #include <errno.h>
29 #include <dirent.h>
30 #include <sys/mman.h>
31 #include <linux/ashmem.h>
32 #include <cutils/ashmem.h>
34 #include "JackError.h"
36 #include <semaphore.h>
39 #define MEMORY_SIZE 10*1024
41 #define SEMAPHORE_NULL_CHAR '\0'
43 // remove ALOGI log
44 #undef ALOGI
45 #define ALOGI
47 namespace android {
50 int AndroidShm::instantiate() {
51 defaultServiceManager()->addService(String16("com.samsung.android.jam.IAndroidShm"), new AndroidShm); // SINGLETON WITH SAME NAME
52 return 0;
55 int AndroidShm::sendCommand(const char* command) {
56 ALOGI("I(pid:%d) send command is %s\n", getpid(), command);
57 if(strcmp(command, "semaphore") == 0) {
58 // print debug message about semaphore simulation
59 for(int i = MAX_SEMAPHORE_MEMORY_COUNT -1 ; i >= 0; i--) {
60 printf("index[%3d] = ptr[%p] name[%s]\n", i, (mSemaphore[i] != NULL)?mSemaphore[i]->getBase():0, mSemaphoreName[i]);
61 ALOGI("index[%3d] = ptr[%p] name[%s]\n", i, (mSemaphore[i] != NULL)?mSemaphore[i]->getBase():0, mSemaphoreName[i]);
64 return NO_ERROR;
67 int AndroidShm::testGetBufferByNewProcess() {
68 ALOGI("testGetBufferByNewProcess...");
69 int status;
70 int childPid = fork();
72 if(childPid > 0) {
73 ALOGI("I(pid%d) made a child process(pid:%d)", getpid(), childPid);
74 ALOGI("I(pid%d) wait until child(%d) was finish", getpid(), childPid);
75 wait(&status);
76 // wait ÇÏÁö ¾ÊÀ¸¸é child process°¡ ³²¾Æ ÀÖÀ½.
77 ALOGI("child(%d) was finished. ", childPid);
78 } else if(childPid == 0) {
79 ALOGI("im a new child process(pid:%d) ", getpid());
80 if(-1 == execlp("/system/bin/getbufferclient","getbufferclient",NULL)) {
81 ALOGE("failed to execute getbufferclient");
83 exit(0);
84 } else {
85 ALOGI("failed creating child process");
87 return 0;
90 int AndroidShm::testGetBuffer() {
91 ALOGI("I(pid:%d) trying to test get buffer...", getpid());
92 sp<IServiceManager> sm = defaultServiceManager();
93 ALOGI("get default ServiceManager is done");
94 sp<IBinder> b;
95 //String16* serviceName = new String16("com.samsung.android.jam.IAndroidShm");
96 do {
97 //ALOGI("here");
98 b = sm->getService(String16("com.samsung.android.jam.IAndroidShm"));
99 //ALOGI("getservice is done");
101 if(b != 0)
102 break;
103 //ALOGI("AndroidShm is not working, waiting...");
104 usleep(500000);
106 } while(true);
108 sp<IAndroidShm> service = interface_cast<IAndroidShm>(b);
110 //shared buffer.
111 sp<IMemoryHeap> receiverMemBase = service->getBuffer(0);
113 unsigned int *base = (unsigned int *) receiverMemBase->getBase();
114 int ret = 0;
115 if(base != (unsigned int *) -1) {
116 ALOGD("AndroidShm::testGetBuffer base=%p Data=0x%x\n",base, *base);
117 *base = (*base)+1;
118 ret = (unsigned int)(*base);
119 ALOGI("AndroidShm::testGetBuffer base=%p Data=0x%x CHANGED\n",base, *base);
120 receiverMemBase = 0;
121 } else {
122 ALOGE("Error shared memory not available\n");
124 return 0;
127 sp<IMemoryHeap> AndroidShm::getBuffer(int index) {
128 ALOGI("I(pid:%d) getBuffer index:%d", getpid(), index);
129 if(index < 0 || index >= MAX_SHARED_MEMORY_COUNT) {
130 ALOGE("error : out of index [%d]", index);
131 return NULL;
133 return mMemHeap[index];
136 int AndroidShm::MemAlloc(unsigned int size) {
137 ALOGI("try to allocate memory size[%d]", size);
138 for(int i = 0; i < MAX_SHARED_MEMORY_COUNT; i++) {
139 if(mMemHeap[i] == NULL) {
140 mMemHeap[i] = new MemoryHeapBase(size);
141 if(mMemHeap[i] == NULL){
142 ALOGI("fail to alloc, try one more...");
143 continue; // try one more.
145 return i;
148 ALOGE("fail to MemAlloc");
149 return -1; // fail to alloc
152 AndroidShm::AndroidShm() {
153 ALOGI("AndroidShm is created");
154 for(int i = 0; i < MAX_SHARED_MEMORY_COUNT; i++) {
155 mMemHeap[i] = NULL;
157 mRegistryIndex = 10000;
158 //mMemHeap = new MemoryHeapBase(MEMORY_SIZE);
159 //unsigned int *base = (unsigned int*) mMemHeap->getBase();
160 //*base = 0xdeadcafe;//
162 for(int j = 0; j < MAX_SEMAPHORE_MEMORY_COUNT; j++) {
163 mSemaphore[j] = NULL;
164 memset(mSemaphoreName[j], SEMAPHORE_NULL_CHAR, MAX_SEMAPHORE_NAME_LENGTH);
168 AndroidShm::~AndroidShm() {
169 ALOGI("AndroidShm is destroyed");
170 for(int i = 0; i < MAX_SHARED_MEMORY_COUNT; i++) {
171 (mMemHeap[i]).clear();
173 for(int j = 0; j < MAX_SEMAPHORE_MEMORY_COUNT; j++) {
174 (mSemaphore[j]).clear();
178 //status_t AndroidShm::onTransact(uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) {
179 // return BnAndroidShm::onTransact(code, data, reply, flags);
182 int AndroidShm::allocShm(const int size) { // if negative return value is error
183 ALOGI("try to alloc shared memory size[%d]", size);
184 return MemAlloc(size);
187 int AndroidShm::removeShm(const unsigned int index) { // shared memory Á¦°Å
188 ALOGI("try to remove shared memory index[%d]", index);
189 if(index >= MAX_SHARED_MEMORY_COUNT) {
190 ALOGE("remove shared memory: out of index");
191 return -1;
193 (mMemHeap[index]).clear();
194 return 1;
197 int AndroidShm::isAllocated(const unsigned int index) { // allocated ¿©ºÎ È®ÀÎ
198 ALOGI("try to check the memory allocation index[%d]", index);
199 if(index >= MAX_SHARED_MEMORY_COUNT) {
200 ALOGE("shared memory: out of index");
201 return 0;
203 if(mMemHeap[index] == NULL)
204 return 0;
205 else
206 return 1;
209 int AndroidShm::setRegistryIndex(const unsigned int index) {
210 ALOGI("set registry index %d", index);
211 mRegistryIndex = index;
212 return 1;
215 int AndroidShm::getRegistryIndex() {
216 return mRegistryIndex;
219 sp<IMemoryHeap> AndroidShm::InitSemaphore(const char* name) {
220 ALOGI("init semaphore [%s]", name);
221 for(int i = 0; i < MAX_SEMAPHORE_MEMORY_COUNT; i++) {
222 if(mSemaphoreName[i][0] == SEMAPHORE_NULL_CHAR) {
223 mSemaphore[i] = new MemoryHeapBase(sizeof(sem_t));
224 if(mSemaphore[i] == NULL){
225 ALOGI("fail to alloc, try one more...");
226 continue;
228 if(sem_init((sem_t*)(mSemaphore[i]->getBase()), 1, 0) == 0) {
229 strncpy(mSemaphoreName[i], name, MAX_SEMAPHORE_NAME_LENGTH - 1);
230 mSemaphoreName[i][MAX_SEMAPHORE_NAME_LENGTH - 1] = '\0';
231 ALOGI("sem_init success");
232 return mSemaphore[i];
233 } else {
234 (mSemaphore[i]).clear();
235 ALOGE("sem_init failed null returned");
236 return NULL;
238 } else {
239 // find already exist name
240 if(strcmp(mSemaphoreName[i], name) == 0) { // found
241 ALOGI("found - return alread allocated semaphore");
242 return mSemaphore[i];
246 ALOGE("sem_init failed null returned 2");
247 return NULL;