Created
November 10, 2016 13:57
-
-
Save azaika/1f36182334b4f3baa99a953415ab0903 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdexcept> | |
#include <string> | |
#include <tuple> | |
#include "Pasolib.hpp" | |
#include "felicalib.h" | |
#pragma comment(lib, "felicalib.lib") | |
namespace pasolib { | |
namespace detail { | |
template < | |
typename T, | |
std::size_t N, | |
std::size_t... I | |
> | |
constexpr std::array<T, N> toArrayImpl( | |
T const(&arr)[N], | |
std::index_sequence<I...> | |
) { | |
return{ { arr[I]... } }; | |
} | |
template <typename T, std::size_t N> | |
constexpr auto toArray(T const(&arr)[N]) { | |
return toArrayImpl( | |
arr, | |
std::make_index_sequence<N>() | |
); | |
} | |
} // ::pasolib::detail | |
void Felica::deleteFelica(::strfelica* ptr) { | |
felica_free(ptr); | |
} | |
Felica::Felica(::strfelica* f) { | |
if (f) | |
feli = FelicaPtr(f, deleteFelica); | |
else | |
feli = nullptr; | |
} | |
std::array<std::uint8_t, 8> Felica::getIDm() const { | |
return detail::toArray(feli->IDm); | |
} | |
void Pasori::deletePasori(void* ptr) { | |
::pasori_close(static_cast<::pasori*>(ptr)); | |
} | |
Pasori::Pasori(bool initImmediately) { | |
if (initImmediately) { | |
try { | |
init(); | |
} | |
catch (std::exception& exp) { | |
throw exp; | |
} | |
} | |
} | |
void Pasori::init() { | |
::pasori* p = ::pasori_open(nullptr); | |
if (!p) | |
throw std::runtime_error("failed to pasori_open."); | |
if (int error = ::pasori_init(p)) | |
throw std::runtime_error( | |
"failed to pasori_init\nerror code: " + std::to_string(error) + "." | |
); | |
paso = PasoriHandle(p, deletePasori); | |
} | |
Felica Pasori::polling(PollingMode pollingMode, std::uint8_t timeSlot) { | |
return | |
::felica_polling( | |
static_cast<::pasori*>(paso.get()), | |
static_cast<std::uint16_t>(pollingMode), | |
0, | |
timeSlot | |
); | |
} | |
} // ::pasolib |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include <memory> | |
#include <array> | |
struct strfelica; | |
namespace pasolib { | |
using PasoriHandle = void*; | |
class Felica { | |
private: | |
static void deleteFelica(::strfelica*); | |
using FelicaPtr = std::unique_ptr<::strfelica, decltype(&deleteFelica)>; | |
FelicaPtr feli = FelicaPtr(nullptr, deleteFelica); | |
public: | |
Felica() = default; | |
Felica(Felica&&) = default; | |
Felica& operator = (Felica&&) = default; | |
Felica(::strfelica*); | |
// FeliCaのIDm(製造ID)を取得します | |
std::array<std::uint8_t, 8> getIDm() const; | |
explicit operator bool() const { | |
return feli != nullptr; | |
} | |
}; | |
enum class PollingMode : std::uint16_t { | |
Any = 0xffff, | |
Edy = 0xfe00, | |
Suica = 0x0003 | |
}; | |
class Pasori { | |
private: | |
static void deletePasori(void*); | |
using PasoriHandle = std::unique_ptr<void, decltype(&deletePasori)>; | |
PasoriHandle paso = PasoriHandle(nullptr, deletePasori); | |
public: | |
// initImmediately: trueの場合、生成と同時にinit()します | |
Pasori(bool initImmediately = false); | |
Pasori(Pasori&&) = default; | |
Pasori& operator = (Pasori&&) = default; | |
// PaSoRiを初期化します | |
// 失敗した場合、std::runtime_error例外を送出します | |
void init(); | |
// PaSoRiにタッチされたFeliCaの情報を返します | |
Felica polling(PollingMode pollingMode, std::uint8_t timeSlot); | |
explicit operator bool() const { | |
return paso != nullptr; | |
} | |
}; | |
} // ::pasolib |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment