Device class implementation
diff --git a/btcore/Android.mk b/btcore/Android.mk
index 3375d02..ec5fe6a 100644
--- a/btcore/Android.mk
+++ b/btcore/Android.mk
@@ -28,6 +28,7 @@
 LOCAL_SRC_FILES := \
     src/bdaddr.c \
     src/counter.c \
+    src/device_class.c \
     src/module.c \
     src/property.c \
     src/uuid.c
@@ -51,11 +52,13 @@
 LOCAL_SRC_FILES := \
     ./test/bdaddr_test.cpp \
     ./test/counter_test.cpp \
+    ./test/device_class_test.cpp \
     ./test/uuid_test.cpp \
     ../osi/test/AllocationTestHarness.cpp
 
 LOCAL_CFLAGS := -Wall -Werror -Werror=unused-variable
 LOCAL_MODULE := net_test_btcore
+
 LOCAL_MODULE_TAGS := tests
 LOCAL_SHARED_LIBRARIES := liblog
 LOCAL_STATIC_LIBRARIES := libbtcore libosi
diff --git a/btcore/include/device_class.h b/btcore/include/device_class.h
new file mode 100644
index 0000000..8c2c3c0
--- /dev/null
+++ b/btcore/include/device_class.h
@@ -0,0 +1,92 @@
+/******************************************************************************
+ *
+ *  Copyright (C) 2014 Google, Inc.
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at:
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ *
+ ******************************************************************************/
+
+#pragma once
+
+#include <stdbool.h>
+#include <stdint.h>
+
+// Provides Class Of Device primitive as specified in the bluetooth spec.
+// [Class Of Device](https://www.bluetooth.org/en-us/specification/assigned-numbers/baseband)
+
+// Device class may be defined in other structures.
+// Only use defined methods to manipulate internals.
+typedef struct bt_device_class_t {
+  uint8_t _[3];  // Do not access directly; use methods below.
+} bt_device_class_t;
+
+// Copies the |data| class of device stream into device class |dc|.  |dc|
+// and |data| must not be NULL.
+void device_class_from_stream(bt_device_class_t *dc, const uint8_t *data);
+
+// Serializes the device class |dc| to pointer argument |data| in big endian
+// format.  |len| must contain the buffer size of |data|.  Returns the actual
+// number of bytes copied into |data|.  |dc| and |data| must not be NULL.
+int device_class_to_stream(const bt_device_class_t *dc, uint8_t *data, size_t len);
+
+// Copies the |data| class of device integer into device class |dc|.  |dc|
+// must not be NULL.
+void device_class_from_int(bt_device_class_t *dc, int data);
+
+// Returns the device class |dc| in integer format.  |dc| must not be NULL.
+int device_class_to_int(const bt_device_class_t *dc);
+
+// Compares and returns |true| if two device classes |p1| and |p2| are equal.
+// False otherwise.
+bool device_class_equals(const bt_device_class_t *p1, const bt_device_class_t *p2);
+
+// Copies and returns |true| if the device class was successfully copied from
+//  |p2| into |p1|.  False otherwise.
+bool device_class_copy(bt_device_class_t *dest, const bt_device_class_t *src);
+
+// Query, getters and setters for the major device class.  |dc| must not be NULL.
+int device_class_get_major_device(const bt_device_class_t *dc);
+void device_class_set_major_device(bt_device_class_t *dc, int val);
+
+// Query, getters and setters for the minor device class. |dc| must not be NULL.
+int device_class_get_minor_device(const bt_device_class_t *dc);
+void device_class_set_minor_device(bt_device_class_t *dc, int val);
+
+// Query, getters and setters for the various major service class features.
+// |dc| must not be NULL.
+bool device_class_get_limited(const bt_device_class_t *dc);
+void device_class_set_limited(bt_device_class_t *dc, bool set);
+
+bool device_class_get_positioning(const bt_device_class_t *dc);
+void device_class_set_positioning(bt_device_class_t *dc, bool set);
+
+bool device_class_get_networking(const bt_device_class_t *dc);
+void device_class_set_networking(bt_device_class_t *dc, bool set);
+
+bool device_class_get_rendering(const bt_device_class_t *dc);
+void device_class_set_rendering(bt_device_class_t *dc, bool set);
+
+bool device_class_get_capturing(const bt_device_class_t *dc);
+void device_class_set_capturing(bt_device_class_t *dc, bool set);
+
+bool device_class_get_object_transfer(const bt_device_class_t *dc);
+void device_class_set_object_transfer(bt_device_class_t *dc, bool set);
+
+bool device_class_get_audio(const bt_device_class_t *dc);
+void device_class_set_audio(bt_device_class_t *dc, bool set);
+
+bool device_class_get_telephony(const bt_device_class_t *dc);
+void device_class_set_telephony(bt_device_class_t *dc, bool set);
+
+bool device_class_get_information(const bt_device_class_t *dc);
+void device_class_set_information(bt_device_class_t *dc, bool set);
diff --git a/btcore/src/device_class.c b/btcore/src/device_class.c
new file mode 100644
index 0000000..6a72ed1
--- /dev/null
+++ b/btcore/src/device_class.c
@@ -0,0 +1,158 @@
+/******************************************************************************
+ *
+ *  Copyright (C) 2014 Google, Inc.
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at:
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ *
+ ******************************************************************************/
+
+#include <arpa/inet.h>
+#include <assert.h>
+#include <string.h>
+
+#include "btcore/include/device_class.h"
+#include "osi/include/osi.h"
+
+typedef struct _bt_device_class_t {
+  uint32_t unused : 2;          // LSBs
+  uint32_t minor_device : 6;
+  uint32_t major_device : 5;
+  uint32_t major_service : 11;  // MSBs
+} __attribute__ ((__packed__)) _bt_device_class_t;
+
+// Convenience to interpret raw device class bytes.
+#define DC(x) ((_bt_device_class_t *)x)
+
+// Ensure the internal device class implementation and public one
+// have equal size.
+COMPILE_ASSERT(sizeof(_bt_device_class_t) == sizeof(bt_device_class_t));
+
+// [Major Service Classes](https://www.bluetooth.org/en-us/specification/assigned-numbers/baseband)
+enum {
+  DC_LIMITED_DISCOVERABLE_MODE = 0x0001,
+  DC_RESERVED14 = 0x0002,
+  DC_RESERVED15 = 0x0004,
+  DC_POSITIONING = 0x0008,
+  DC_NETWORKING = 0x0010,
+  DC_RENDERING = 0x0020,
+  DC_CAPTURING = 0x0040,
+  DC_OBJECT_TRANSFER = 0x0080,
+  DC_AUDIO = 0x0100,
+  DC_TELEPHONY = 0x0200,
+  DC_INFORMATION = 0x0400,
+};
+
+static bool device_class_get_major_service_(const bt_device_class_t *dc, int bitmask);
+static void device_class_clr_major_service_(bt_device_class_t *dc, int bitmask);
+static void device_class_set_major_service_(bt_device_class_t *dc, int bitmask);
+
+void device_class_from_stream(bt_device_class_t *dc, const uint8_t *data) {
+  assert(dc != NULL);
+  assert(data != NULL);
+  *dc = *(bt_device_class_t *)data;
+}
+
+int device_class_to_stream(const bt_device_class_t *dc, uint8_t *data, size_t len) {
+  assert(dc != NULL);
+  assert(data != NULL);
+  assert(len >= sizeof(bt_device_class_t));
+  for (size_t i = 0; i < sizeof(bt_device_class_t); ++i) {
+    data[i] = dc->_[i];
+  }
+  return sizeof(bt_device_class_t);
+}
+
+void device_class_from_int(bt_device_class_t *dc, int data) {
+  assert(dc != NULL);
+  assert(data != 0);
+  // Careful with endianess.
+  dc->_[0] = data & 0xff;
+  dc->_[1] = (data >> 8) & 0xff;
+  dc->_[2] = (data >> 16) & 0xff;
+}
+
+int device_class_to_int(const bt_device_class_t *dc) {
+  assert(dc != NULL);
+  // Careful with endianess.
+  return (int)(le32toh(*(int*)dc) & 0xffffff);
+}
+
+bool device_class_equals(const bt_device_class_t *p1, const bt_device_class_t *p2) {
+  assert(p1 != NULL);
+  assert(p2 != NULL);
+  return (memcmp(p1, p2, sizeof(bt_device_class_t)) == 0);
+}
+
+bool device_class_copy(bt_device_class_t *dest, const bt_device_class_t *src) {
+  assert(dest != NULL);
+  assert(src != NULL);
+  return (memcpy(dest, src, sizeof(bt_device_class_t)) == dest);
+}
+
+int device_class_get_major_device(const bt_device_class_t *dc) {
+  assert(dc != NULL);
+  return DC(dc)->major_device;
+}
+
+void device_class_set_major_device(bt_device_class_t *dc, int val) {
+  assert(dc != NULL);
+  DC(dc)->major_device = val;
+}
+
+int device_class_get_minor_device(const bt_device_class_t *dc) {
+  assert(dc != NULL);
+  return DC(dc)->minor_device;
+}
+
+void device_class_set_minor_device(bt_device_class_t *dc, int val) {
+  assert(dc != NULL);
+  DC(dc)->minor_device = val;
+}
+
+bool device_class_get_information(const bt_device_class_t *dc) {
+  assert(dc != NULL);
+  return device_class_get_major_service_(dc, DC_INFORMATION);
+}
+
+void device_class_set_information(bt_device_class_t *dc, bool set) {
+  assert(dc != NULL);
+  if (set)
+    device_class_set_major_service_(dc, DC_INFORMATION);
+  else
+    device_class_clr_major_service_(dc, DC_INFORMATION);
+}
+
+bool device_class_get_limited(const bt_device_class_t *dc) {
+  assert(dc != NULL);
+  return device_class_get_major_service_(dc, DC_LIMITED_DISCOVERABLE_MODE);
+}
+
+void device_class_set_limited(bt_device_class_t *dc, bool set) {
+  assert(dc != NULL);
+  if (set)
+    device_class_set_major_service_(dc, DC_LIMITED_DISCOVERABLE_MODE);
+  else
+    device_class_clr_major_service_(dc, DC_LIMITED_DISCOVERABLE_MODE);
+}
+
+static bool device_class_get_major_service_(const bt_device_class_t *dc, int bitmask) {
+  return (DC(dc)->major_service & bitmask);
+}
+
+static void device_class_clr_major_service_(bt_device_class_t *dc, int bitmask) {
+  DC(dc)->major_service &= ~bitmask;
+}
+
+static void device_class_set_major_service_(bt_device_class_t *dc, int bitmask) {
+  DC(dc)->major_service |= bitmask;
+}
diff --git a/btcore/test/device_class_test.cpp b/btcore/test/device_class_test.cpp
new file mode 100644
index 0000000..c3098a2
--- /dev/null
+++ b/btcore/test/device_class_test.cpp
@@ -0,0 +1,220 @@
+/******************************************************************************
+ *
+ *  Copyright (C) 2014 Google, Inc.
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at:
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ *
+ ******************************************************************************/
+
+#include <arpa/inet.h>
+#include <gtest/gtest.h>
+#include "osi/test/AllocationTestHarness.h"
+
+extern "C" {
+#include "btcore/include/device_class.h"
+}  // "C"
+
+// Device Class is 3 bytes.
+static const int DC_MASK = 0xffffff;
+
+::testing::AssertionResult check_bitfield(const char *m_expr,
+    const char *n_expr, int m, int n) {
+  if (m == n)
+    return ::testing::AssertionSuccess();
+
+  std::stringstream ss;
+
+  ss.str("");
+  ss << std::showbase << std::hex << std::setw(8) << std::setfill('0') << m;
+  std::string expected_str = ss.str();
+
+  ss.str("");
+  ss << std::showbase << std::hex << std::setw(8) << std::setfill('0') << n;
+  std::string actual_str = ss.str();
+
+  return ::testing::AssertionFailure() << m_expr << " and " << n_expr
+    << " ( " << expected_str << " vs " << actual_str << " )";
+}
+
+class DeviceClassTest : public AllocationTestHarness {};
+
+TEST_F(DeviceClassTest, cod_sizeof) {
+  uint8_t dc_stream[] = { 0x00, 0x00, 0x00, 0x00};
+  bt_device_class_t dc0;
+  device_class_from_stream(&dc0, dc_stream);
+  EXPECT_EQ((size_t)3, sizeof(dc0));
+}
+
+TEST_F(DeviceClassTest, simple) {
+  uint8_t dc_stream[][sizeof(bt_device_class_t)] = {
+    { 0x00, 0x00, 0x00 },
+    { 0xff, 0xff, 0xff },
+    { 0xaa, 0x55, 0xaa },
+    { 0x01, 0x23, 0x45 },
+    { 0x20, 0x07, 0x14 },
+  };
+
+  for (size_t i = 0; i < sizeof(dc_stream)/sizeof(bt_device_class_t); i++) {
+    bt_device_class_t dc;
+    device_class_from_stream(&dc, (uint8_t*)&dc_stream[i]);
+
+    uint8_t *to_stream = (uint8_t *)&dc;
+    EXPECT_PRED_FORMAT2(check_bitfield, (unsigned)dc_stream[i][0], to_stream[0]);
+    EXPECT_PRED_FORMAT2(check_bitfield, (unsigned)dc_stream[i][1], to_stream[1]);
+    EXPECT_PRED_FORMAT2(check_bitfield, (unsigned)dc_stream[i][2], to_stream[2]);
+  }
+}
+
+TEST_F(DeviceClassTest, to_stream) {
+  {
+    bt_device_class_t dc;
+
+    uint8_t dc_stream0[] = { 0x00, 0x00, 0x00, 0xaa };
+    device_class_from_stream(&dc, dc_stream0);
+
+    uint8_t dc_stream1[] = { 0x00, 0x00, 0x00, 0x00 };
+    int rc = device_class_to_stream(&dc, dc_stream1, sizeof(dc_stream1));
+    EXPECT_EQ(3, rc);
+
+    uint32_t *val = (uint32_t *)&dc;
+    EXPECT_PRED_FORMAT2(check_bitfield, 0x00000000, *val & 0xffffff);
+
+    EXPECT_PRED_FORMAT2(check_bitfield, 0x00, dc_stream1[0]);
+    EXPECT_PRED_FORMAT2(check_bitfield, 0x00, dc_stream1[1]);
+    EXPECT_PRED_FORMAT2(check_bitfield, 0x00, dc_stream1[2]);
+  }
+
+  {
+    uint8_t dc_stream0[] = { 0xaa, 0x55, 0xaa, 0x55 };
+    uint8_t dc_stream1[] = { 0x00, 0x00, 0x00, 0x00 };
+
+    bt_device_class_t dc;
+    device_class_from_stream(&dc, dc_stream0);
+
+    int rc = device_class_to_stream(&dc, dc_stream1, sizeof(dc_stream1));
+    EXPECT_EQ(3, rc);
+    uint32_t *val = (uint32_t *)&dc;
+    EXPECT_PRED_FORMAT2(check_bitfield, 0x00aa55aa, *val & 0xffffff);
+
+    EXPECT_PRED_FORMAT2(check_bitfield, 0xaa, dc_stream1[0]);
+    EXPECT_PRED_FORMAT2(check_bitfield, 0x55, dc_stream1[1]);
+    EXPECT_PRED_FORMAT2(check_bitfield, 0xaa, dc_stream1[2]);
+  }
+
+  {
+    uint8_t dc_stream0[] = { 0x01, 0x23, 0x45, 0x67 };
+    uint8_t dc_stream1[] = { 0x00, 0x00, 0x00, 0x00 };
+
+    bt_device_class_t dc;
+    device_class_from_stream(&dc, dc_stream0);
+
+    int rc = device_class_to_stream(&dc, dc_stream1, sizeof(dc_stream1));
+    EXPECT_EQ(3, rc);
+    uint32_t *val = (uint32_t *)&dc;
+    EXPECT_PRED_FORMAT2(check_bitfield, 0x452301, *val & 0xffffff);
+
+    EXPECT_PRED_FORMAT2(check_bitfield, 0x01, dc_stream1[0]);
+    EXPECT_PRED_FORMAT2(check_bitfield, 0x23, dc_stream1[1]);
+    EXPECT_PRED_FORMAT2(check_bitfield, 0x45, dc_stream1[2]);
+  }
+}
+
+TEST_F(DeviceClassTest, limited_discoverable_mode) {
+  uint8_t dc_stream[] = { 0x00, 0x00, 0x00 };
+  bt_device_class_t dc;
+  device_class_from_stream(&dc, dc_stream);
+  uint32_t *test = (uint32_t *)&dc;
+
+  EXPECT_FALSE(device_class_get_limited(&dc));
+  EXPECT_EQ((unsigned)0x00000000, *test & DC_MASK);
+
+  device_class_set_limited(&dc, true);
+  EXPECT_TRUE(device_class_get_limited(&dc));
+  EXPECT_EQ((unsigned)0x00002000, *test & DC_MASK);
+
+  device_class_set_limited(&dc, false);
+  EXPECT_FALSE(device_class_get_limited(&dc));
+  EXPECT_EQ((unsigned)0x00000000, *test & DC_MASK);
+
+  device_class_set_limited(&dc, true);
+  EXPECT_PRED_FORMAT2(check_bitfield, 0x00002000, *test & DC_MASK);
+
+  device_class_set_limited(&dc, false);
+  EXPECT_PRED_FORMAT2(check_bitfield, 0x00000000, *test & DC_MASK);
+}
+
+TEST_F(DeviceClassTest, equals) {
+  uint8_t dc_stream0[] = { 0x00, 0x01, 0x02 };
+  uint8_t dc_stream1[] = { 0x00, 0x02, 0x03 };
+
+  bt_device_class_t dc0;
+  device_class_from_stream(&dc0, dc_stream0);
+  bt_device_class_t dc1;
+  device_class_from_stream(&dc1, dc_stream1);
+  EXPECT_FALSE(device_class_equals(&dc0, &dc1));
+}
+
+TEST_F(DeviceClassTest, copy) {
+  uint8_t dc_stream0[] = { 0xaa, 0x55, 0x33 };
+  bt_device_class_t dc0;
+  device_class_from_stream(&dc0, dc_stream0);
+  bt_device_class_t dc1;
+  EXPECT_TRUE(device_class_copy(&dc1, &dc0));
+  EXPECT_TRUE(device_class_equals(&dc0, &dc1));
+}
+
+TEST_F(DeviceClassTest, from_int) {
+  bt_device_class_t dc1;
+  int cod1 = 0x5a020c;  // 5898764
+  device_class_from_int(&dc1, cod1);
+
+  uint8_t dc_stream[] = { 0x0c, 0x02, 0x5a };
+  bt_device_class_t dc2;
+  device_class_from_stream(&dc2, dc_stream);
+  EXPECT_TRUE(device_class_equals(&dc1, &dc2));
+}
+
+TEST_F(DeviceClassTest, to_int) {
+  bt_device_class_t dc1 = {{ 0x0c, 0x02, 0x5a }};
+  int cod1 = device_class_to_int(&dc1);
+
+  EXPECT_EQ(dc1._[0], 0x0c);
+  EXPECT_EQ(dc1._[1], 0x02);
+  EXPECT_EQ(dc1._[2], 0x5a);
+
+  bt_device_class_t dc2;
+  uint8_t dc_stream[] = { 0x0c, 0x02, 0x5a };
+  device_class_from_stream(&dc2, dc_stream);
+
+  EXPECT_EQ(dc2._[0], 0x0c);
+  EXPECT_EQ(dc2._[1], 0x02);
+  EXPECT_EQ(dc2._[2], 0x5a);
+
+  int cod2 = device_class_to_int(&dc2);
+  EXPECT_EQ(cod1, cod2);
+  EXPECT_EQ(cod1, 0x5a020c);  // 5898764
+}
+
+TEST_F(DeviceClassTest, endian) {
+  bt_device_class_t dc;
+  int cod1 = 0x200714;  // 2098964
+  device_class_from_int(&dc, cod1);
+
+  EXPECT_EQ(dc._[0], 0x14);
+  EXPECT_EQ(dc._[1], 0x07);
+  EXPECT_EQ(dc._[2], 0x20);
+
+  int cod2 = device_class_to_int(&dc);
+  EXPECT_EQ(cod1, cod2);
+  EXPECT_EQ(cod2, 0x200714); // 2098964
+}