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