ioacademy-jikim 3 years ago
parent
commit
7c461cd04c

BIN
03_day.pptx View File


+ 18
- 0
03_day/binder_5/Android.bp View File

@@ -0,0 +1,18 @@
1
+cc_binary {
2
+    name: "my_server_cpp_3",
3
+    shared_libs: ["liblog", "libutils", "libbinder"],
4
+    srcs: [
5
+        "my_server.cpp",
6
+        "ILedService.cpp",
7
+        "LedService.cpp",
8
+    ],
9
+}
10
+
11
+cc_binary {
12
+    name: "my_client_cpp_3",
13
+    shared_libs: ["liblog", "libutils", "libbinder"],
14
+    srcs: [
15
+        "my_client.cpp",
16
+        "ILedService.cpp",
17
+    ],
18
+}

+ 46
- 0
03_day/binder_5/ILedService.cpp View File

@@ -0,0 +1,46 @@
1
+#define LOG_TAG "LedService"
2
+
3
+#include "ILedService.h"
4
+
5
+#include <binder/IPCThreadState.h>
6
+#include <binder/Parcel.h>
7
+#include <unistd.h>
8
+
9
+namespace android {
10
+
11
+// ----------------------------------------------------------------------
12
+
13
+class BpLedService : public BpInterface<ILedService>
14
+{
15
+public:
16
+    explicit BpLedService(const sp<IBinder>& impl)
17
+        : BpInterface<ILedService>(impl)
18
+    {
19
+    }
20
+
21
+    virtual void ledOn(void)
22
+    {
23
+		printf("BpLedService::ledOn()\n");
24
+        Parcel data, reply;
25
+        remote()->transact(LED_ON, data, &reply);
26
+    }
27
+};
28
+
29
+IMPLEMENT_META_INTERFACE(LedService, "android.os.ILedService");
30
+
31
+// ----------------------------------------------------------------------
32
+
33
+status_t BnLedService::onTransact(
34
+    uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
35
+{
36
+	switch (code) {
37
+		case LED_ON: {
38
+						 ledOn();
39
+						 return NO_ERROR;
40
+					 } break;
41
+		default:
42
+					 return BBinder::onTransact(code, data, reply, flags);
43
+	}
44
+}
45
+// ----------------------------------------------------------------------
46
+}; // namespace android

+ 41
- 0
03_day/binder_5/ILedService.h View File

@@ -0,0 +1,41 @@
1
+#ifndef ANDROID_ILED_SERVICE_H
2
+#define ANDROID_ILED_SERVICE_H
3
+
4
+#include <binder/IInterface.h>
5
+#include <utils/String16.h>
6
+
7
+namespace android {
8
+
9
+// ----------------------------------------------------------------------
10
+
11
+class ILedService : public IInterface
12
+{
13
+public:
14
+    DECLARE_META_INTERFACE(LedService)
15
+
16
+    virtual void   ledOn() = 0;
17
+
18
+    enum {
19
+        LED_ON = IBinder::FIRST_CALL_TRANSACTION
20
+    };
21
+};
22
+
23
+// ----------------------------------------------------------------------
24
+
25
+class BnLedService : public BnInterface<ILedService>
26
+{
27
+public:
28
+    virtual status_t    onTransact( uint32_t code,
29
+                                    const Parcel& data,
30
+                                    Parcel* reply,
31
+                                    uint32_t flags = 0);
32
+};
33
+
34
+// ----------------------------------------------------------------------------
35
+}; // namespace android
36
+
37
+#endif // ANDROID_ISERVICE_MANAGER_H
38
+
39
+
40
+
41
+

+ 10
- 0
03_day/binder_5/LedService.cpp View File

@@ -0,0 +1,10 @@
1
+#include "LedService.h"
2
+#include <stdio.h> 
3
+
4
+namespace android 
5
+{
6
+	void LedService::ledOn(void)
7
+	{
8
+		printf("LedService::ledOn()\n");
9
+	}
10
+};

+ 13
- 0
03_day/binder_5/LedService.h View File

@@ -0,0 +1,13 @@
1
+#ifndef ANDROID_LED_SERVICE_H
2
+#define ANDROID_LED_SERVICE_H
3
+#include "ILedService.h"
4
+
5
+namespace android {
6
+	class LedService : public BnLedService
7
+	{
8
+		public : 
9
+			void ledOn(void);
10
+	};
11
+};
12
+
13
+#endif

+ 54
- 0
03_day/binder_5/ProcessState.cpp View File

@@ -0,0 +1,54 @@
1
+#if 1
2
+#include <stdio.h>
3
+class ProcessState
4
+{
5
+	static ProcessState *gProcess;
6
+	ProcessState()
7
+	{
8
+		printf("fd=open(\"/dev/binder\", O_RDWR)\n");
9
+	}
10
+	public:
11
+	static ProcessState *self()
12
+	{
13
+		if( gProcess == 0 )
14
+			gProcess = new ProcessState;
15
+		return gProcess;
16
+	}
17
+	~ProcessState()
18
+	{
19
+		printf("close(fd)\n");
20
+	}
21
+};
22
+ProcessState *ProcessState::gProcess = 0;
23
+
24
+int main()
25
+{
26
+	ProcessState *p1 = ProcessState::self();
27
+	ProcessState *p2 = ProcessState::self();
28
+	//ProcessState process; 
29
+	return 0;
30
+}
31
+#endif
32
+
33
+#if 0
34
+#include <stdio.h>
35
+class ProcessState
36
+{
37
+	public:
38
+		ProcessState()
39
+		{
40
+			printf("fd=open(\"/dev/binder\", O_RDWR)\n");
41
+		}
42
+		~ProcessState()
43
+		{
44
+			printf("close(fd)\n");
45
+		}
46
+};
47
+
48
+int main()
49
+{
50
+	ProcessState p1;
51
+	ProcessState p2;
52
+	return 0;
53
+}
54
+#endif

+ 23
- 0
03_day/binder_5/my_client.cpp View File

@@ -0,0 +1,23 @@
1
+
2
+#include <binder/ProcessState.h>
3
+#include <binder/IServiceManager.h>
4
+#include <binder/Binder.h>
5
+#include <binder/IPCThreadState.h>
6
+#include <utils/String16.h>
7
+#include "ILedService.h"
8
+
9
+using namespace android;
10
+
11
+int main()
12
+{
13
+	sp<ProcessState> proc(ProcessState::self());
14
+	sp<IServiceManager> sm(defaultServiceManager());
15
+	sp<IBinder> binder = sm->getService( String16("led.service") );
16
+	sp<ILedService> led = interface_cast<ILedService>( binder );
17
+	led->ledOn();
18
+
19
+	return 0;
20
+}
21
+
22
+
23
+

+ 22
- 0
03_day/binder_5/my_server.cpp View File

@@ -0,0 +1,22 @@
1
+
2
+#include <binder/ProcessState.h>
3
+#include <binder/IServiceManager.h>
4
+#include <binder/Binder.h>
5
+#include <binder/IPCThreadState.h>
6
+#include <utils/String16.h>
7
+#include "LedService.h"
8
+
9
+using namespace android;
10
+
11
+int main()
12
+{
13
+	sp<ProcessState> proc(ProcessState::self());
14
+	sp<IServiceManager> sm(defaultServiceManager());
15
+	sm->addService( String16("led.service"), new LedService );
16
+	IPCThreadState::self()->joinThreadPool();
17
+
18
+	return 0;
19
+}
20
+
21
+
22
+

+ 18
- 0
03_day/binder_6/Android.bp View File

@@ -0,0 +1,18 @@
1
+cc_binary {
2
+    name: "my_server_cpp_3",
3
+    shared_libs: ["liblog", "libutils", "libbinder"],
4
+    srcs: [
5
+        "my_server.cpp",
6
+        "ILedService.cpp",
7
+        "LedService.cpp",
8
+    ],
9
+}
10
+
11
+cc_binary {
12
+    name: "my_client_cpp_3",
13
+    shared_libs: ["liblog", "libutils", "libbinder"],
14
+    srcs: [
15
+        "my_client.cpp",
16
+        "ILedService.cpp",
17
+    ],
18
+}

+ 49
- 0
03_day/binder_6/ILedService.cpp View File

@@ -0,0 +1,49 @@
1
+#define LOG_TAG "LedService"
2
+
3
+#include "ILedService.h"
4
+
5
+#include <binder/IPCThreadState.h>
6
+#include <binder/Parcel.h>
7
+#include <unistd.h>
8
+
9
+namespace android {
10
+
11
+// ----------------------------------------------------------------------
12
+
13
+class BpLedService : public BpInterface<ILedService>
14
+{
15
+public:
16
+    explicit BpLedService(const sp<IBinder>& impl)
17
+        : BpInterface<ILedService>(impl)
18
+    {
19
+    }
20
+
21
+    virtual void connect(sp<ILedClient>& client)
22
+    {
23
+		printf("BpLedService::ledOn()\n");
24
+        Parcel data, reply;
25
+		data.writeStrongBinder(ILedClient::asBinder(client));
26
+        remote()->transact(CONNECT, data, &reply);
27
+    }
28
+};
29
+
30
+IMPLEMENT_META_INTERFACE(LedService, "android.os.ILedService");
31
+
32
+// ----------------------------------------------------------------------
33
+
34
+status_t BnLedService::onTransact(
35
+    uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
36
+{
37
+	switch (code) {
38
+		case CONNECT: {
39
+						 sp<ICameraClient> client; 
40
+						 data.readStrongBinder(&client);
41
+						 connect(client);
42
+						 return NO_ERROR;
43
+					 } break;
44
+		default:
45
+					 return BBinder::onTransact(code, data, reply, flags);
46
+	}
47
+}
48
+// ----------------------------------------------------------------------
49
+}; // namespace android

+ 42
- 0
03_day/binder_6/ILedService.h View File

@@ -0,0 +1,42 @@
1
+#ifndef ANDROID_ILED_SERVICE_H
2
+#define ANDROID_ILED_SERVICE_H
3
+
4
+#include <binder/IInterface.h>
5
+#include <utils/String16.h>
6
+#include "ILedClient.h"
7
+
8
+namespace android {
9
+
10
+// ----------------------------------------------------------------------
11
+
12
+class ILedService : public IInterface
13
+{
14
+public:
15
+    DECLARE_META_INTERFACE(LedService)
16
+
17
+    virtual void   connect(sp<ILedClient>& client) = 0;
18
+
19
+    enum {
20
+        CONNECT = IBinder::FIRST_CALL_TRANSACTION
21
+    };
22
+};
23
+
24
+// ----------------------------------------------------------------------
25
+
26
+class BnLedService : public BnInterface<ILedService>
27
+{
28
+public:
29
+    virtual status_t    onTransact( uint32_t code,
30
+                                    const Parcel& data,
31
+                                    Parcel* reply,
32
+                                    uint32_t flags = 0);
33
+};
34
+
35
+// ----------------------------------------------------------------------------
36
+}; // namespace android
37
+
38
+#endif // ANDROID_ISERVICE_MANAGER_H
39
+
40
+
41
+
42
+

+ 11
- 0
03_day/binder_6/LedService.cpp View File

@@ -0,0 +1,11 @@
1
+#include "LedService.h"
2
+#include <stdio.h> 
3
+
4
+namespace android 
5
+{
6
+	void LedService::connect(sp<ILedClient>& client)
7
+	{
8
+		printf("LedService::ledOn()\n");
9
+		client->dataCallback(222);
10
+	}
11
+};

+ 13
- 0
03_day/binder_6/LedService.h View File

@@ -0,0 +1,13 @@
1
+#ifndef ANDROID_LED_SERVICE_H
2
+#define ANDROID_LED_SERVICE_H
3
+#include "ILedService.h"
4
+
5
+namespace android {
6
+	class LedService : public BnLedService
7
+	{
8
+		public : 
9
+    		void connect(sp<ILedClient>& client);
10
+	};
11
+};
12
+
13
+#endif

+ 54
- 0
03_day/binder_6/ProcessState.cpp View File

@@ -0,0 +1,54 @@
1
+#if 1
2
+#include <stdio.h>
3
+class ProcessState
4
+{
5
+	static ProcessState *gProcess;
6
+	ProcessState()
7
+	{
8
+		printf("fd=open(\"/dev/binder\", O_RDWR)\n");
9
+	}
10
+	public:
11
+	static ProcessState *self()
12
+	{
13
+		if( gProcess == 0 )
14
+			gProcess = new ProcessState;
15
+		return gProcess;
16
+	}
17
+	~ProcessState()
18
+	{
19
+		printf("close(fd)\n");
20
+	}
21
+};
22
+ProcessState *ProcessState::gProcess = 0;
23
+
24
+int main()
25
+{
26
+	ProcessState *p1 = ProcessState::self();
27
+	ProcessState *p2 = ProcessState::self();
28
+	//ProcessState process; 
29
+	return 0;
30
+}
31
+#endif
32
+
33
+#if 0
34
+#include <stdio.h>
35
+class ProcessState
36
+{
37
+	public:
38
+		ProcessState()
39
+		{
40
+			printf("fd=open(\"/dev/binder\", O_RDWR)\n");
41
+		}
42
+		~ProcessState()
43
+		{
44
+			printf("close(fd)\n");
45
+		}
46
+};
47
+
48
+int main()
49
+{
50
+	ProcessState p1;
51
+	ProcessState p2;
52
+	return 0;
53
+}
54
+#endif

+ 25
- 0
03_day/binder_6/my_client.cpp View File

@@ -0,0 +1,25 @@
1
+
2
+#include <binder/ProcessState.h>
3
+#include <binder/IServiceManager.h>
4
+#include <binder/Binder.h>
5
+#include <binder/IPCThreadState.h>
6
+#include <utils/String16.h>
7
+#include "ILedService.h"
8
+
9
+using namespace android;
10
+
11
+int main()
12
+{
13
+	sp<ProcessState> proc(ProcessState::self());
14
+	sp<IServiceManager> sm(defaultServiceManager());
15
+	sp<IBinder> binder = sm->getService( String16("led.service") );
16
+	sp<ILedService> led = interface_cast<ILedService>( binder );
17
+	led->connect( new Led );
18
+	IPCThreadState::self()->joinThreadPool();
19
+	
20
+
21
+	return 0;
22
+}
23
+
24
+
25
+

+ 22
- 0
03_day/binder_6/my_server.cpp View File

@@ -0,0 +1,22 @@
1
+
2
+#include <binder/ProcessState.h>
3
+#include <binder/IServiceManager.h>
4
+#include <binder/Binder.h>
5
+#include <binder/IPCThreadState.h>
6
+#include <utils/String16.h>
7
+#include "LedService.h"
8
+
9
+using namespace android;
10
+
11
+int main()
12
+{
13
+	sp<ProcessState> proc(ProcessState::self());
14
+	sp<IServiceManager> sm(defaultServiceManager());
15
+	sm->addService( String16("led.service"), new LedService );
16
+	IPCThreadState::self()->joinThreadPool();
17
+
18
+	return 0;
19
+}
20
+
21
+
22
+